public function processMatrix($originField, $field)
 {
     $settings = new MatrixSettingsModel($field);
     // Get the original Matrix Blocks
     $blockTypes = craft()->matrix->getBlockTypesByFieldId($originField->id);
     $newBlockTypes = array();
     foreach ($blockTypes as $originBlockType) {
         $newBlockType = new MatrixBlockTypeModel();
         // New Matrix Block
         $newBlockType->fieldId = $field->id;
         $newBlockType->name = $originBlockType->name;
         $newBlockType->handle = $originBlockType->handle;
         $newBlockFields = array();
         foreach ($originBlockType->fields as $originField) {
             $newBlockField = new FieldModel();
             // New Matrix Block field
             $newBlockField->name = $originField->name;
             $newBlockField->handle = $originField->handle;
             $newBlockField->required = $originField->required;
             $newBlockField->translatable = $originField->translatable;
             $newBlockField->type = $originField->type;
             $newBlockField->settings = $originField->settings;
             $newBlockFields[] = $newBlockField;
         }
         $newBlockType->setFields($newBlockFields);
         craft()->matrix->saveBlockType($newBlockType);
         $newBlockTypes[] = $newBlockType;
     }
     $settings->setBlockTypes($newBlockTypes);
     craft()->matrix->saveSettings($settings);
 }
 /**
  * @inheritDoc ISavableComponentType::prepSettings()
  *
  * @param array $settings
  *
  * @return array
  */
 public function prepSettings($settings)
 {
     if ($settings instanceof MatrixSettingsModel) {
         return $settings;
     }
     $matrixSettings = new MatrixSettingsModel($this->model);
     $blockTypes = array();
     if (!empty($settings['blockTypes'])) {
         foreach ($settings['blockTypes'] as $blockTypeId => $blockTypeSettings) {
             $blockType = new MatrixBlockTypeModel();
             $blockType->id = $blockTypeId;
             $blockType->fieldId = $this->model->id;
             $blockType->name = $blockTypeSettings['name'];
             $blockType->handle = $blockTypeSettings['handle'];
             $fields = array();
             if (!empty($blockTypeSettings['fields'])) {
                 foreach ($blockTypeSettings['fields'] as $fieldId => $fieldSettings) {
                     $field = new FieldModel();
                     $field->id = $fieldId;
                     $field->name = $fieldSettings['name'];
                     $field->handle = $fieldSettings['handle'];
                     $field->instructions = $fieldSettings['instructions'];
                     $field->required = !empty($fieldSettings['required']);
                     $field->translatable = !empty($fieldSettings['translatable']);
                     $field->type = $fieldSettings['type'];
                     if (isset($fieldSettings['typesettings'])) {
                         $field->settings = $fieldSettings['typesettings'];
                     }
                     $fields[] = $field;
                 }
             }
             $blockType->setFields($fields);
             $blockTypes[] = $blockType;
         }
     }
     $matrixSettings->setBlockTypes($blockTypes);
     if (!empty($settings['maxBlocks'])) {
         $matrixSettings->maxBlocks = $settings['maxBlocks'];
     }
     return $matrixSettings;
 }
Exemple #3
0
 /**
  * Validates a Matrix field's settings.
  *
  * If the settings don’t validate, any validation errors will be stored on the settings model.
  *
  * @param MatrixSettingsModel $settings The settings model.
  *
  * @return bool Whether the settings validated.
  */
 public function validateFieldSettings(MatrixSettingsModel $settings)
 {
     $validates = true;
     $this->_uniqueBlockTypeAndFieldHandles = array();
     $uniqueAttributes = array('name', 'handle');
     $uniqueAttributeValues = array();
     foreach ($settings->getBlockTypes() as $blockType) {
         if (!$this->validateBlockType($blockType, false)) {
             // Don't break out of the loop because we still want to get validation errors for the remaining block
             // types.
             $validates = false;
         }
         // Do our own unique name/handle validation, since the DB-based validation can't be trusted when saving
         // multiple records at once
         foreach ($uniqueAttributes as $attribute) {
             $value = $blockType->{$attribute};
             if ($value && (!isset($uniqueAttributeValues[$attribute]) || !in_array($value, $uniqueAttributeValues[$attribute]))) {
                 $uniqueAttributeValues[$attribute][] = $value;
             } else {
                 $blockType->addError($attribute, Craft::t('{attribute} "{value}" has already been taken.', array('attribute' => $blockType->getAttributeLabel($attribute), 'value' => HtmlHelper::encode($value))));
                 $validates = false;
             }
         }
     }
     return $validates;
 }
 /**
  * Validates a Matrix field's settings.
  *
  * If the settings don’t validate, any validation errors will be stored on the settings model.
  *
  * @param MatrixSettingsModel $settings The settings model.
  *
  * @return bool Whether the settings validated.
  */
 public function validateFieldSettings(MatrixSettingsModel $settings)
 {
     $validates = true;
     $this->_uniqueBlockTypeAndFieldHandles = array();
     foreach ($settings->getBlockTypes() as $blockType) {
         if (!$this->validateBlockType($blockType)) {
             // Don't break out of the loop because we still want to get validation errors for the remaining block
             // types.
             $validates = false;
         }
     }
     return $validates;
 }