/** * Saves a field layout for a given pimped block type */ public function actionSaveFieldLayout() { $this->requirePostRequest(); $this->requireAjaxRequest(); $pimpedBlockTypeId = craft()->request->getPost('pimpedBlockTypeId'); $blockTypeFieldLayouts = craft()->request->getPost('blockTypeFieldLayouts'); if ($pimpedBlockTypeId) { $pimpedBlockTypeRecord = PimpMyMatrix_BlockTypeRecord::model()->findById($pimpedBlockTypeId); if (!$pimpedBlockTypeRecord) { throw new Exception(Craft::t('No PimpMyMatrix block type exists with the ID “{id}”', array('id' => $pimpedBlockTypeId))); } $pimpedBlockType = PimpMyMatrix_BlockTypeModel::populateModel($pimpedBlockTypeRecord); } else { return false; } // Set the field layout on the model $postedFieldLayout = craft()->request->getPost('blockTypeFieldLayouts', array()); $assembledLayout = craft()->fields->assembleLayout($postedFieldLayout, array()); $pimpedBlockType->setFieldLayout($assembledLayout); // Save it if (craft()->pimpMyMatrix_blockTypes->saveFieldLayout($pimpedBlockType)) { $this->returnJson(array('success' => true)); } else { $this->returnJson(array('success' => false)); } }
/** * Populates a PimpMyMatrix_BlockTypeModel with attributes from a PimpMyMatrix_BlockTypeRecord. * * @param PimpMyMatrix_BlockTypeRecord|null * * @return PimpMyMatrix_BlockTypeModel|null */ private function _populateBlockTypeFromRecord($blockTypeRecord) { if (!$blockTypeRecord) { return null; } $blockType = PimpMyMatrix_BlockTypeModel::populateModel($blockTypeRecord); // Use the fieldId to get the field and save the handle on to the model $matrixField = craft()->fields->getFieldById($blockType->fieldId); $blockType->fieldHandle = $matrixField->handle; // Save the MatrixBlockTypeModel on to our model $blockType->matrixBlockType = $blockType->getBlockType(); // Save the field layout content on to our model $layout = $blockType->getFieldLayout(); $fields = array(); foreach ($layout->getFields() as $field) { $fields[] = array('tabId' => $field->tabId, 'sortOrder' => $field->sortOrder, 'field' => $field->getField()); } $blockType->fieldLayout = array('tabs' => $layout->getTabs(), 'fields' => $fields); return $blockType; }