Skip to content

Commit

Permalink
Merge pull request #3 from xodigital/matrix-block-types
Browse files Browse the repository at this point in the history
Matrix block type import & export
  • Loading branch information
Marty Wallace committed Jan 11, 2015
2 parents 00beebe + be4fc55 commit 34438a1
Showing 1 changed file with 111 additions and 19 deletions.
130 changes: 111 additions & 19 deletions artvandelay/services/ArtVandelay_FieldsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,34 @@ public function export(array $groups)
'type' => $field->type,
'settings' => $field->settings
);

if ($field->type == 'Matrix')
{
$blockTypeDefs = array();

$blockTypes = craft()->matrix->getBlockTypesByFieldId($field->id);
foreach ($blockTypes as $blockType)
{
$blockTypeFieldDefs = array();

foreach ($blockType->getFields() as $blockTypeField)
{
$blockTypeFieldDefs[$blockTypeField->handle] = array(
'name' => $blockTypeField->name,
'required' => $blockTypeField->required,
'translatable' => $blockTypeField->translatable,
'type' => $blockTypeField->type
);
}

$blockTypeDefs[$blockType->handle] = array(
'name' => $blockType->name,
'fields' => $blockTypeFieldDefs
);
}

$fieldDefs[$field->handle]['blockTypes'] = $blockTypeDefs;
}
}

$groupDefs[$group->name] = $fieldDefs;
Expand Down Expand Up @@ -49,7 +77,6 @@ public function import($groupDefs)

$groups = craft()->fields->getAllGroups('name');
$fields = craft()->fields->getAllFields('handle');
$fieldTypes = craft()->fields->getAllFieldTypes();

foreach ($groupDefs as $groupName => $fieldDefs)
{
Expand All @@ -66,26 +93,91 @@ public function import($groupDefs)

foreach ($fieldDefs as $fieldHandle => $fieldDef)
{
if (array_key_exists($fieldDef['type'], $fieldTypes))
$field = array_key_exists($fieldHandle, $fields)
? $fields[$fieldHandle]
: new FieldModel();

$field->setAttributes(array(
'handle' => $fieldHandle,
'groupId' => $group->id,
'name' => $fieldDef['name'],
'context' => $fieldDef['context'],
'instructions' => $fieldDef['instructions'],
'translatable' => $fieldDef['translatable'],
'type' => $fieldDef['type'],
'settings' => $fieldDef['settings']
));

if (!$field->getFieldType())
{
$field = array_key_exists($fieldHandle, $fields)
? $fields[$fieldHandle]
: new FieldModel();

$field->setAttributes(array(
'handle' => $fieldHandle,
'groupId' => $group->id,
'name' => $fieldDef['name'],
'context' => $fieldDef['context'],
'instructions' => $fieldDef['instructions'],
'translatable' => $fieldDef['translatable'],
'type' => $fieldDef['type'],
'settings' => $fieldDef['settings']
));

if (!craft()->fields->saveField($field))
if ($field->type == 'Matrix')
{
return $result->error("One of the field's types does not exist. Are you missing a plugin?");
}
else
{
return $result->error("Field type '$field->type' does not exist. Are you missing a plugin?");
}
}

if (!craft()->fields->saveField($field))
{
return $result->error($field->getAllErrors());
}

if ($field->type == 'Matrix')
{
$blockTypes = craft()->matrix->getBlockTypesByFieldId($field->id, 'handle');

if (!array_key_exists('blockTypes', $fieldDef))
{
return $result->error('`fields[handle].blockTypes` must exist');
}

foreach ($fieldDef['blockTypes'] as $blockTypeHandle => $blockTypeDef)
{
return $result->error($field->getAllErrors());
$blockType = array_key_exists($blockTypeHandle, $blockTypes)
? $blockTypes[$blockTypeHandle]
: new MatrixBlockTypeModel();

$blockType->fieldId = $field->id;
$blockType->name = $blockTypeDef['name'];
$blockType->handle = $blockTypeHandle;

if (!array_key_exists('fields', $blockTypeDef))
{
return $result->error('`fields[handle].blockTypes[handle].fields` must exist');
}

$blockTypeFields = array();
foreach ($blockType->getFields() as $blockTypeField)
{
$blockTypeFields[$blockTypeField->handle] = $blockTypeField;
}

$newBlockTypeFields = array();

foreach ($blockTypeDef['fields'] as $blockTypeFieldHandle => $blockTypeFieldDef)
{
$blockTypeField = array_key_exists($blockTypeFieldHandle, $blockTypeFields)
? $blockTypeFields[$blockTypeFieldHandle]
: new FieldModel();

$blockTypeField->name = $blockTypeFieldDef['name'];
$blockTypeField->handle = $blockTypeFieldHandle;
$blockTypeField->required = $blockTypeFieldDef['required'];
$blockTypeField->translatable = $blockTypeFieldDef['translatable'];
$blockTypeField->type = $blockTypeFieldDef['type'];

$newBlockTypeFields[] = $blockTypeField;
}

$blockType->setFields($newBlockTypeFields);

if (!craft()->matrix->saveBlockType($blockType))
{
return $result->error($blockType->getAllErrors());
}
}
}
}
Expand Down

0 comments on commit 34438a1

Please sign in to comment.