From: Vincent Vanwaelscappel Date: Wed, 19 May 2021 10:09:46 +0000 (+0200) Subject: wip #4464 @2 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=461be09c1c0c416da5a127ed45c57690bcfd3c08;p=cubist_cms-back.git wip #4464 @2 --- diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index 34e2af5..2b2daa6 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -151,6 +151,13 @@ class Field implements \ArrayAccess $this->init(); } + /** + * @return string + */ + public function getName(){ + return $this->getAttribute('name'); + } + public function getRules() { return $this->getAttribute('rules', ''); diff --git a/src/app/Magic/Fields/Files.php b/src/app/Magic/Fields/Files.php index 9177edc..c9809cf 100644 --- a/src/app/Magic/Fields/Files.php +++ b/src/app/Magic/Fields/Files.php @@ -12,17 +12,19 @@ class Files extends Field protected $_adminType = 'dropzone_media'; protected $_viewNamespace = CubistBackpackServiceProvider::NAMESPACE . '::fields'; protected $_databaseType = 'text'; - protected $_thumbCollection = 'backpack_thumb'; + protected $_thumbConversion = 'backpack_thumb'; + protected $_nameAsCollection = false; protected $_maxFiles = 1; public function getDefaultAttributes() { $collection = '_TBD_'; return array_merge(parent::getDefaultAttributes(), [ + 'name_as_collection' => $this->_nameAsCollection, 'form' => 'update', 'mime_types' => $this->_mimeTypes, 'collection' => $collection, - 'thumb_collection' => $this->_thumbCollection, + 'thumb_conversion' => $this->_thumbConversion, 'default' => $collection, 'options' => [ @@ -36,6 +38,16 @@ class Files extends Field ]); } + protected function _postSetAttributes() + { + parent::_postSetAttributes(); + if ($this->getAttribute('name_as_collection')) { + $name = $this->getAttribute('name'); + $this->setAttribute('collection', $name); + $this->setAttribute('default', $name); + } + } + protected function _getAttributesAliases() { return array_merge(parent::_getAttributesAliases(), [ diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index f5ab86a..0cc7058 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -655,7 +655,12 @@ class CubistMagicAbstractModel extends Model implements HasMedia $collection = $this->getAttribute($fieldname); $save = false; if (!$collection) { - $collection = substr(hash('sha256', uniqid($fieldname, true)), 0, 10); + $field = $this->getField($fieldname); + if ($field->getAttribute('name_as_collection', false)) { + $collection = $fieldname; + } else { + $collection = substr(hash('sha256', uniqid($fieldname, true)), 0, 10); + } $this->setAttribute($fieldname, $collection); $save = true; } diff --git a/src/resources/views/fields/dropzone_media.blade.php b/src/resources/views/fields/dropzone_media.blade.php index 6f5f19d..34a4dc2 100644 --- a/src/resources/views/fields/dropzone_media.blade.php +++ b/src/resources/views/fields/dropzone_media.blade.php @@ -171,7 +171,7 @@ $collection = old(square_brackets_to_dots($field['name'])) ?? $field['value'] ?? } try { - $thumb_url = $media->getUrl($field['thumb_collection'] ?? null); + $thumb_url = $media->getUrl($field['thumb_conversion'] ?? null); } catch (Exception $e) { $thumb_url = ''; }