public function prepSettings($settings) { if ($settings instanceof SuperTable_SettingsModel) { return $settings; } $superTableSettings = new SuperTable_SettingsModel($this->model); $blockTypes = array(); $columns = array(); if (!empty($settings['blockTypes'])) { foreach ($settings['blockTypes'] as $blockTypeId => $blockTypeSettings) { $blockType = new SuperTable_BlockTypeModel(); $blockType->id = $blockTypeId; $blockType->fieldId = $this->model->id; $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['width'])) { $columns[$field->id] = array('width' => $fieldSettings['width']); } if (isset($fieldSettings['typesettings'])) { $field->settings = $fieldSettings['typesettings']; } $fields[] = $field; } } $blockType->setFields($fields); $blockTypes[] = $blockType; } } $superTableSettings->setBlockTypes($blockTypes); // Save additional field column data - but in the SuperTable field $superTableSettings->columns = $columns; if (!empty($settings['fieldLayout'])) { $superTableSettings->fieldLayout = $settings['fieldLayout']; } if (!empty($settings['staticField'])) { $superTableSettings->staticField = $settings['staticField']; } if (!empty($settings['selectionLabel'])) { $superTableSettings->selectionLabel = $settings['selectionLabel']; } if (!empty($settings['maxRows'])) { $superTableSettings->maxRows = $settings['maxRows']; } return $superTableSettings; }
public function handleSuperTableImport($fieldDef, $field) { $blockTypes = craft()->superTable->getBlockTypesByFieldId($field->id, 'id'); foreach ($fieldDef['blockTypes'] as $blockTypeFields) { $blockType = new SuperTable_BlockTypeModel(); $blockType->fieldId = $field->id; $newBlockTypeFields = array(); $extraFields = array(); foreach ($blockTypeFields as $blockTypeFieldHandle => $blockTypeFieldDef) { $blockTypeField = new FieldModel(); $blockTypeField->name = $blockTypeFieldDef['name']; $blockTypeField->handle = $blockTypeFieldHandle; $blockTypeField->required = $blockTypeFieldDef['required']; $blockTypeField->translatable = $blockTypeFieldDef['translatable']; $blockTypeField->type = $blockTypeFieldDef['type']; $blockTypeField->settings = $blockTypeFieldDef['settings']; $newBlockTypeFields[] = $blockTypeField; if ($blockTypeField->type == 'Matrix') { $extraFields[] = array($blockTypeFieldDef, $blockTypeField); } if ($blockTypeField->type == 'PositionSelect') { $this->handlePositionSelect($blockTypeFieldDef, $blockTypeField); } } $blockType->setFields($newBlockTypeFields); if (!craft()->superTable->saveBlockType($blockType)) { return $blockType->getAllErrors(); } foreach ($extraFields as $options) { $this->handleMatrixImport($options[0], $options[1]); } } }
private function _getBlockTypeRecord(SuperTable_BlockTypeModel $blockType) { if (!$blockType->isNew()) { $blockTypeId = $blockType->id; if (!isset($this->_blockTypeRecordsById) || !array_key_exists($blockTypeId, $this->_blockTypeRecordsById)) { $this->_blockTypeRecordsById[$blockTypeId] = SuperTable_BlockTypeRecord::model()->findById($blockTypeId); if (!$this->_blockTypeRecordsById[$blockTypeId]) { throw new Exception(Craft::t('No block type exists with the ID “{id}”.', array('id' => $blockTypeId))); } } return $this->_blockTypeRecordsById[$blockTypeId]; } else { return new SuperTable_BlockTypeRecord(); } }