FieldsService

Packagecraft.app.services
NamespaceCraft
Inheritanceclass FieldsService » BaseApplicationComponent » \CApplicationComponent » \CComponent
Implements\IApplicationComponent
Since1.0
Source Codecraft/app/services/FieldsService.php

Class FieldsService

Public Properties #

PropertyTypeDescriptionDefined By
allFieldTypesarrayReturns all installed fieldtypes.FieldsService
allFieldsFieldModel[]Returns all fields within a field context(s).FieldsService
allGroupsarrayReturns all field groups.FieldsService
behaviorsarraythe behaviors that should be attached to this component.\CApplicationComponent
fieldsWithContentarrayReturns all fields that have a column in the content table.FieldsService
isInitializedboolChecks if this application component has been initialized.\CApplicationComponent
oldFieldColumnPrefixstringFieldsService

Public Methods #

MethodDescriptionDefined By
__call()Calls the named method which is not a class method.\CComponent
__get()Returns a property value, an event handler list or a behavior based on its name.\CComponent
__isset()Checks if a property value is null.\CComponent
__set()Sets value of a component property.\CComponent
__unset()Sets a component property to be null.\CComponent
asa()Returns the named behavior object.\CComponent
assembleLayout()Assembles a field layout.FieldsService
assembleLayoutFromPost()Assembles a field layout from post data.FieldsService
attachBehavior()Attaches a behavior to this component.\CComponent
attachBehaviors()Attaches a list of behaviors to the component.\CComponent
attachEventHandler()Attaches an event handler to an event.\CComponent
canGetProperty()Determines whether a property can be read.\CComponent
canSetProperty()Determines whether a property can be set.\CComponent
deleteField()Deletes a field.FieldsService
deleteFieldById()Deletes a field by its ID.FieldsService
deleteGroupById()Deletes a field group.FieldsService
deleteLayoutById()Deletes a field layout(s) by its ID.FieldsService
deleteLayoutsByType()Deletes field layouts of a given type.FieldsService
detachBehavior()Detaches a behavior from the component.\CComponent
detachBehaviors()Detaches all behaviors from the component.\CComponent
detachEventHandler()Detaches an existing event handler.\CComponent
disableBehavior()Disables an attached behavior.\CComponent
disableBehaviors()Disables all behaviors attached to this component.\CComponent
doesFieldWithHandleExist()Returns whether a field exists with a given handle and context.FieldsService
enableBehavior()Enables an attached behavior.\CComponent
enableBehaviors()Enables all behaviors attached to this component.\CComponent
evaluateExpression()Evaluates a PHP expression or callback under the context of this component.\CComponent
getAllFieldTypes()Returns all installed fieldtypes.FieldsService
getAllFields()Returns all fields within a field context(s).FieldsService
getAllGroups()Returns all field groups.FieldsService
getEventHandlers()Returns the list of attached event handlers for an event.\CComponent
getFieldByHandle()Returns a field by its handle.FieldsService
getFieldById()Returns a field by its ID.FieldsService
getFieldType()Gets a fieldtype.FieldsService
getFieldsByElementType()Returns all of the fields used by a given element type.FieldsService
getFieldsByGroupId()Returns all the fields in a given group.FieldsService
getFieldsWithContent()Returns all fields that have a column in the content table.FieldsService
getGroupById()Returns a field group by its ID.FieldsService
getIsInitialized()Checks if this application component has been initialized.\CApplicationComponent
getLayoutById()Returns a field layout by its ID.FieldsService
getLayoutByType()Returns a field layout by its type.FieldsService
getLayoutFieldsById()Returns a layout's fields by its ID.FieldsService
getLayoutTabsById()Returns a layout's tabs by its ID.FieldsService
getOrderedLayoutFieldsById()Returns a layout's fields by its ID, in the layout-defined sort order.FieldsService
hasEvent()Determines whether an event is defined.\CComponent
hasEventHandler()Checks whether the named event has attached handlers.\CComponent
hasProperty()Determines whether a property is defined.\CComponent
init()Initializes the application component.\CApplicationComponent
isInitialized()Checks if this application component has been initialized yet, or not.BaseApplicationComponent
onSaveFieldLayout()Fires an 'onSaveFieldLayout' event.FieldsService
populateFieldType()Populates a fieldtype by a field model.FieldsService
raiseEvent()Raises an event.\CComponent
saveField()Saves a field.FieldsService
saveGroup()Saves a field group.FieldsService
saveLayout()Saves a field layout.FieldsService
validateField()Validates a field's settings.FieldsService

Events #

EventDescriptionDefined By
onSaveFieldLayoutFires an 'onSaveFieldLayout' event.FieldsService

Property Details #

allFieldTypes #

read-only
public array getAllFieldTypes()

Returns all installed fieldtypes.

allFields #

read-only
public FieldModel[] getAllFields(string|null $indexBy = null, array $context = null)

Returns all fields within a field context(s).

allGroups #

read-only
public array getAllGroups(string|null $indexBy = null)

Returns all field groups.

fieldsWithContent #

read-only
public array getFieldsWithContent()

Returns all fields that have a column in the content table.

oldFieldColumnPrefix #

public string $oldFieldColumnPrefix;

Method Details #

assembleLayout() #

public function assembleLayout($postedFieldLayout, $requiredFields = array())
{
     $tabs = array();
     $fields = array();

     $tabSortOrder = 0;

     foreach ($postedFieldLayout as $tabName => $fieldIds)
     {
          $tabFields = array();
          $tabSortOrder++;

          foreach ($fieldIds as $fieldSortOrder => $fieldId)
          {
               $field = new FieldLayoutFieldModel();
               $field->fieldId   = $fieldId;
               $field->required  = in_array($fieldId, $requiredFields);
               $field->sortOrder = ($fieldSortOrder+1);

               $fields[] = $field;
               $tabFields[] = $field;
          }

          $tab = new FieldLayoutTabModel();
          $tab->name      = urldecode($tabName);
          $tab->sortOrder = $tabSortOrder;
          $tab->setFields($tabFields);

          $tabs[] = $tab;
     }

     $layout = new FieldLayoutModel();
     $layout->setTabs($tabs);
     $layout->setFields($fields);

     return $layout;
}
$postedFieldLayoutarray
$requiredFieldsarray
ReturnsFieldLayoutModel

Assembles a field layout.

assembleLayoutFromPost() #

public function assembleLayoutFromPost($namespace = null)
{
     $paramPrefix = ($namespace ? rtrim($namespace, '.').'.' : '');
     $postedFieldLayout = craft()->request->getPost($paramPrefix.'fieldLayout', array());
     $requiredFields = craft()->request->getPost($paramPrefix.'requiredFields', array());

     return $this->assembleLayout($postedFieldLayout, $requiredFields);
}
$namespacestring / nullThe namespace that the form data was posted in, if any.
ReturnsFieldLayoutModel

Assembles a field layout from post data.

deleteField() #

public function deleteField(FieldModel $field)
{
     $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
     try
     {
          $fieldType = $field->getFieldType();

          if ($fieldType)
          {
               $field->getFieldType()->onBeforeDelete();
          }

          // De we need to delete the content column?
          $contentTable = craft()->content->contentTable;
          $fieldColumnPrefix = craft()->content->fieldColumnPrefix;

          if (craft()->db->columnExists($contentTable, $fieldColumnPrefix.$field->handle))
          {
               craft()->db->createCommand()->dropColumn($contentTable, $fieldColumnPrefix.$field->handle);
          }

          // Delete the row in fields
          $affectedRows = craft()->db->createCommand()->delete('fields', array('id' => $field->id));

          if ($affectedRows)
          {
               if ($fieldType)
               {
                    $field->getFieldType()->onAfterDelete();
               }

               $context = craft()->content->fieldContext;

               // Nuke any memoized value.
               if (isset($this->_fieldsByContextAndHandle[$context]) && isset($this->_fieldsByContextAndHandle[$context][$field->handle]))
               {
                    unset($this->_fieldsByContextAndHandle[$context][$field->handle]);
               }
          }

          if ($transaction !== null)
          {
               $transaction->commit();
          }

          return (bool) $affectedRows;
     }
     catch (\Exception $e)
     {
          if ($transaction !== null)
          {
               $transaction->rollback();
          }

          throw $e;
     }
}
$fieldFieldModel
Returnsbool

Deletes a field.

deleteFieldById() #

public function deleteFieldById($fieldId)
{
     $fieldRecord = FieldRecord::model()->findById($fieldId);

     if (!$fieldRecord)
     {
          return false;
     }

     $field = FieldModel::populateModel($fieldRecord);
     return $this->deleteField($field);
}
$fieldIdint
Returnsbool

Deletes a field by its ID.

deleteGroupById() #

public function deleteGroupById($groupId)
{
     $groupRecord = FieldGroupRecord::model()->with('fields')->findById($groupId);

     if (!$groupRecord)
     {
          return false;
     }

     // Manually delete the fields (rather than relying on cascade deletes) so we have a chance to delete the
     // content columns
     foreach ($groupRecord->fields as $fieldRecord)
     {
          $field = FieldModel::populateModel($fieldRecord);
          $this->deleteField($field);
     }

     $affectedRows = craft()->db->createCommand()->delete('fieldgroups', array('id' => $groupId));
     return (bool) $affectedRows;
}
$groupIdint
Returnsbool

Deletes a field group.

deleteLayoutById() #

public function deleteLayoutById($layoutId)
{
     if (!$layoutId)
     {
          return false;
     }

     if (is_array($layoutId))
     {
          $layoutId = array_filter($layoutId);

          if (empty($layoutId))
          {
               return false;
          }

          $affectedRows = craft()->db->createCommand()->delete('fieldlayouts', array('in', 'id', $layoutId));
     }
     else
     {
          $affectedRows = craft()->db->createCommand()->delete('fieldlayouts', array('id' => $layoutId));
     }

     return (bool) $affectedRows;
}
$layoutIdint / array
Returnsbool

Deletes a field layout(s) by its ID.

deleteLayoutsByType() #

public function deleteLayoutsByType($type)
{
     $affectedRows = craft()->db->createCommand()->delete('fieldlayouts', array('type' => $type));
     return (bool) $affectedRows;
}
$typestring
Returnsbool

Deletes field layouts of a given type.

doesFieldWithHandleExist() #

public function doesFieldWithHandleExist($handle, $context = null)
{
     if ($context === null)
     {
          $context = craft()->content->fieldContext;
     }

     if (!isset($this->_allFieldHandlesByContext))
     {
          $this->_allFieldHandlesByContext = array();

          $results = craft()->db->createCommand()
               ->select('handle,context')
               ->from('fields')
               ->queryAll();

          foreach ($results as $result)
          {
               $this->_allFieldHandlesByContext[$result['context']][] = $result['handle'];
          }
     }

     return (isset($this->_allFieldHandlesByContext[$context]) && in_array($handle, $this->_allFieldHandlesByContext[$context]));
}
$handlestringThe field handle
$contextstring / nullThe field context (defauts to ContentService::$fieldContext)
Returnsbool

Whether a field with that handle exists

Returns whether a field exists with a given handle and context.

getAllFieldTypes() #

public function getAllFieldTypes()
{
     return craft()->components->getComponentsByType(ComponentType::Field);
}
Returnsarray

Returns all installed fieldtypes.

getAllFields() #

public function getAllFields($indexBy = null, $context = null)
{
     if ($context === null)
     {
          $context = array(craft()->content->fieldContext);
     }
     else if (!is_array($context))
     {
          $context = array($context);
     }

     $missingContexts = array();

     foreach ($context as $c)
     {
          if (!isset($this->_allFieldsInContext[$c]))
          {
               $missingContexts[] = $c;
               $this->_allFieldsInContext[$c] = array();
          }
     }

     if (!empty($missingContexts))
     {
          $rows = $this->_createFieldQuery()
               ->where(array('in', 'f.context', $missingContexts))
               ->queryAll();

          foreach ($rows as $row)
          {
               $field = $this->_populateField($row);

               $this->_allFieldsInContext[$row['context']][] = $field;
               $this->_fieldsById[$field->id] = $field;
               $this->_fieldsByContextAndHandle[$row['context']][$field->handle] = $field;
          }
     }

     $fields = array();

     foreach ($context as $c)
     {
          if (!$indexBy)
          {
               $fields = array_merge($fields, $this->_allFieldsInContext[$c]);
          }
          else
          {
               foreach ($this->_allFieldsInContext[$c] as $field)
               {
                    $fields[$field->$indexBy] = $field;
               }
          }
     }

     return $fields;
}
$indexBystring / nullThe field property to index the resulting fields by
$contextarrayThe field context(s) to fetch fields from. Defaults to ContentService::$fieldContext.
ReturnsFieldModel[]

The resulting fields

Returns all fields within a field context(s).

getAllGroups() #

public function getAllGroups($indexBy = null)
{
     if (!$this->_fetchedAllGroups)
     {
          $this->_groupsById = array();

          $results = $this->_createGroupQuery()->queryAll();

          foreach ($results as $result)
          {
               $group = new FieldGroupModel($result);
               $this->_groupsById[$group->id] = $group;
          }

          $this->_fetchedAllGroups = true;
     }

     if ($indexBy == 'id')
     {
          $groups = $this->_groupsById;
     }
     else if (!$indexBy)
     {
          $groups = array_values($this->_groupsById);
     }
     else
     {
          $groups = array();

          foreach ($this->_groupsById as $group)
          {
               $groups[$group->$indexBy] = $group;
          }
     }

     return $groups;
}
$indexBystring / null
Returnsarray

Returns all field groups.

getFieldByHandle() #

public function getFieldByHandle($handle)
{
     $context = craft()->content->fieldContext;

     if (!isset($this->_fieldsByContextAndHandle[$context]) || !array_key_exists($handle, $this->_fieldsByContextAndHandle[$context]))
     {
          // Guilty until proven innocent
          $this->_fieldsByContextAndHandle[$context][$handle] = null;

          if ($this->doesFieldWithHandleExist($handle, $context))
          {
               $result = $this->_createFieldQuery()
                    ->where(array('and', 'f.handle = :handle', 'f.context = :context'), array(':handle' => $handle, ':context' => $context))
                    ->queryRow();

               if ($result)
               {
                    $field = $this->_populateField($result);
                    $this->_fieldsById[$field->id] = $field;
                    $this->_fieldsByContextAndHandle[$context][$field->handle] = $field;
               }
          }
     }

     return $this->_fieldsByContextAndHandle[$context][$handle];
}
$handlestring
ReturnsFieldModel / null

Returns a field by its handle.

getFieldById() #

public function getFieldById($fieldId)
{
     if (!isset($this->_fieldsById) || !array_key_exists($fieldId, $this->_fieldsById))
     {
          $result = $this->_createFieldQuery()
               ->where('f.id = :id', array(':id' => $fieldId))
               ->queryRow();

          if ($result)
          {
               $field = $this->_populateField($result);

               $this->_fieldsById[$field->id] = $field;
               $this->_fieldsByContextAndHandle[$field->context][$field->handle] = $field;
          }
          else
          {
               return null;
          }
     }

     return $this->_fieldsById[$fieldId];
}
$fieldIdint
ReturnsFieldModel / null

Returns a field by its ID.

getFieldType() #

public function getFieldType($class)
{
     return craft()->components->getComponentByTypeAndClass(ComponentType::Field, $class);
}
$classstring
ReturnsBaseFieldType / null

Gets a fieldtype.

getFieldsByElementType() #

public function getFieldsByElementType($elementTypeClass, $indexBy = null)
{
     $results = $this->_createFieldQuery()
          ->join('fieldlayoutfields flf', 'flf.fieldId = f.id')
          ->join('fieldlayouts fl', 'fl.id = flf.layoutId')
          ->where('fl.type = :type', array(':type' => $elementTypeClass))
          ->queryAll();

     $fields = array();

     foreach ($results as $result)
     {
          $field = $this->_populateField($result);

          if ($indexBy)
          {
               $fields[$field->$indexBy] = $field;
          }
          else
          {
               $fields[] = $field;
          }
     }

     return $fields;
}
$elementTypeClassstring
$indexBystring / null
ReturnsFieldModel[]

Returns all of the fields used by a given element type.

getFieldsByGroupId() #

public function getFieldsByGroupId($groupId, $indexBy = null)
{
     $results = $this->_createFieldQuery()
          ->where('f.groupId = :groupId', array(':groupId' => $groupId))
          ->queryAll();

     $fields = array();

     foreach ($results as $result)
     {
          $field = $this->_populateField($result);

          if ($indexBy)
          {
               $fields[$field->$indexBy] = $field;
          }
          else
          {
               $fields[] = $field;
          }
     }

     return $fields;
}
$groupIdint
$indexBystring / null
Returnsarray

Returns all the fields in a given group.

getFieldsWithContent() #

public function getFieldsWithContent()
{
     $context = craft()->content->fieldContext;

     if (!isset($this->_fieldsWithContent[$context]))
     {
          $this->_fieldsWithContent[$context] = array();

          foreach ($this->getAllFields() as $field)
          {
               if ($field->hasContentColumn())
               {
                    $this->_fieldsWithContent[$context][] = $field;
               }
          }
     }

     return $this->_fieldsWithContent[$context];
}
Returnsarray

Returns all fields that have a column in the content table.

getGroupById() #

public function getGroupById($groupId)
{
     if (!isset($this->_groupsById) || !array_key_exists($groupId, $this->_groupsById))
     {
          $result = $this->_createGroupQuery()
               ->where('id = :id', array(':id' => $groupId))
               ->queryRow();

          if ($result)
          {
               $group = new FieldGroupModel($result);
          }
          else
          {
               $group = null;
          }

          $this->_groupsById[$groupId] = $group;
     }

     return $this->_groupsById[$groupId];
}
$groupIdint
ReturnsFieldGroupModel / null

Returns a field group by its ID.

getLayoutById() #

public function getLayoutById($layoutId)
{
     if (!isset($this->_layoutsById) || !array_key_exists($layoutId, $this->_layoutsById))
     {
          $result = $this->_createLayoutQuery()
               ->where('id = :id', array(':id' => $layoutId))
               ->queryRow();

          if ($result)
          {
               $layout = new FieldLayoutModel($result);
          }
          else
          {
               $layout = null;
          }

          $this->_layoutsById[$layoutId] = $layout;
     }

     return $this->_layoutsById[$layoutId];
}
$layoutIdint
ReturnsFieldLayoutModel / null

Returns a field layout by its ID.

getLayoutByType() #

public function getLayoutByType($type)
{
     if (!isset($this->_layoutsByType) || !array_key_exists($type, $this->_layoutsByType))
     {
          $result = $this->_createLayoutQuery()
               ->where('type = :type', array(':type' => $type))
               ->queryRow();

          if ($result)
          {
               $id = $result['id'];

               if (!isset($this->_layoutsById[$id]))
               {
                    $this->_layoutsById[$id] = new FieldLayoutModel($result);
               }

               $layout = $this->_layoutsById[$id];
          }
          else
          {
               $layout = new FieldLayoutModel();
          }

          $this->_layoutsByType[$type] = $layout;
     }

     return $this->_layoutsByType[$type];
}
$typestring
ReturnsFieldLayoutModel

Returns a field layout by its type.

getLayoutFieldsById() #

public function getLayoutFieldsById($layoutId)
{
     $results = $this->_createLayoutFieldQuery($layoutId)->queryAll();

     return FieldLayoutFieldModel::populateModels($results);
}
$layoutIdint
Returnsarray

Returns a layout's fields by its ID.

getLayoutTabsById() #

public function getLayoutTabsById($layoutId)
{
     $results = $this->_createLayoutTabQuery()
          ->where('layoutId = :layoutId', array(':layoutId' => $layoutId))
          ->queryAll();

     return FieldLayoutTabModel::populateModels($results);
}
$layoutIdint
Returnsarray

Returns a layout's tabs by its ID.

getOrderedLayoutFieldsById() #

public function getOrderedLayoutFieldsById($layoutId)
{
     $results = $this->_createLayoutFieldQuery($layoutId)
          ->join('fieldlayouttabs fieldlayouttabs', 'fieldlayouttabs.id = fieldlayoutfields.tabId')
          ->order('fieldlayouttabs.sortOrder, fieldlayoutfields.sortOrder')
          ->queryAll();

     return FieldLayoutFieldModel::populateModels($results);
}
$layoutIdint
Returnsarray

Returns a layout's fields by its ID, in the layout-defined sort order.

onSaveFieldLayout() #

public function onSaveFieldLayout(Event $event)
{
     $this->raiseEvent('onSaveFieldLayout', $event);
}
$eventEvent

Fires an 'onSaveFieldLayout' event.

populateFieldType() #

public function populateFieldType(FieldModel $field, $element = null)
{
     $fieldType = craft()->components->populateComponentByTypeAndModel(ComponentType::Field, $field);

     if ($fieldType)
     {
          $fieldType->element = $element;
          return $fieldType;
     }
}
$fieldFieldModel
$elementBaseElementModel / null
ReturnsBaseFieldType / null

Populates a fieldtype by a field model.

saveField() #

public function saveField(FieldModel $field, $validate = true)
{
     if (!$validate || $this->validateField($field))
     {
          $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
          try
          {
               $field->context = craft()->content->fieldContext;

               $fieldRecord = $this->_getFieldRecord($field);
               $isNewField = $fieldRecord->isNewRecord();

               // Get the field type
               $fieldType = $field->getFieldType();

               // Create/alter the content table column
               $columnType = $fieldType->defineContentAttribute();

               $contentTable = craft()->content->contentTable;
               $oldColumnName = $this->oldFieldColumnPrefix.$fieldRecord->getOldHandle();
               $newColumnName = craft()->content->fieldColumnPrefix.$field->handle;

               if ($columnType)
               {
                    $columnType = ModelHelper::normalizeAttributeConfig($columnType);

                    // Make sure we're working with the latest data in the case of a renamed field.
                    craft()->db->schema->refresh();

                    if (craft()->db->columnExists($contentTable, $oldColumnName))
                    {
                         craft()->db->createCommand()->alterColumn($contentTable, $oldColumnName, $columnType, $newColumnName);
                    }
                    else if (craft()->db->columnExists($contentTable, $newColumnName))
                    {
                         craft()->db->createCommand()->alterColumn($contentTable, $newColumnName, $columnType);
                    }
                    else
                    {
                         craft()->db->createCommand()->addColumn($contentTable, $newColumnName, $columnType);
                    }
               }
               else
               {
                    // Did the old field have a column we need to remove?
                    if (!$isNewField)
                    {
                         if ($fieldRecord->getOldHandle() && craft()->db->columnExists($contentTable, $oldColumnName))
                         {
                              craft()->db->createCommand()->dropColumn($contentTable, $oldColumnName);
                         }
                    }
               }

               $fieldRecord->groupId      = $field->groupId;
               $fieldRecord->name         = $field->name;
               $fieldRecord->handle       = $field->handle;
               $fieldRecord->context      = $field->context;
               $fieldRecord->instructions = $field->instructions;
               $fieldRecord->translatable = $field->translatable;
               $fieldRecord->type         = $field->type;

               // Give the field type a chance to prep the settings from post
               $preppedSettings = $fieldType->prepSettings($field->settings);

               // Set the prepped settings on the FieldRecord, FieldModel, and the field type
               $fieldRecord->settings = $field->settings = $preppedSettings;
               $fieldType->setSettings($preppedSettings);

               if ($fieldRecord->settings instanceof BaseModel)
               {
                    // Call getAttributes() without passing 'true' so the __model__ isn't saved
                    $fieldRecord->settings = $fieldRecord->settings->getAttributes();
               }

               $fieldType->onBeforeSave();
               $fieldRecord->save(false);

               // Now that we have a field ID, save it on the model
               if ($isNewField)
               {
                    $field->id = $fieldRecord->id;
               }

               if (!$isNewField)
               {
                    // Save the old field handle on the model in case the field type needs to do something with it.
                    $field->oldHandle = $fieldRecord->getOldHandle();

                    unset($this->_fieldsByContextAndHandle[$field->context][$field->oldHandle]);

                    if (
                         isset($this->_allFieldHandlesByContext[$field->context]) &&
                         $field->oldHandle != $field->handle &&
                         ($oldHandleIndex = array_search($field->oldHandle, $this->_allFieldHandlesByContext[$field->context])) !== false
                    )
                    {
                         array_splice($this->_allFieldHandlesByContext[$field->context], $oldHandleIndex, 1);
                    }
               }

               // Cache it
               $this->_fieldsById[$field->id] = $field;
               $this->_fieldsByContextAndHandle[$field->context][$field->handle] = $field;

               if (isset($this->_allFieldHandlesByContext))
               {
                    $this->_allFieldHandlesByContext[$field->context][] = $field->handle;
               }

               unset($this->_allFieldsInContext[$field->context]);
               unset($this->_fieldsWithContent[$field->context]);

               $fieldType->onAfterSave();

               if ($transaction !== null)
               {
                    $transaction->commit();
               }
          }
          catch (\Exception $e)
          {
               if ($transaction !== null)
               {
                    $transaction->rollback();
               }

               throw $e;
          }

          return true;
     }
     else
     {
          return false;
     }
}
$fieldFieldModel
$validatebool
Returnsbool

Saves a field.

saveGroup() #

public function saveGroup(FieldGroupModel $group)
{
     $groupRecord = $this->_getGroupRecord($group);
     $groupRecord->name = $group->name;

     if ($groupRecord->validate())
     {
          $groupRecord->save(false);

          // Now that we have an ID, save it on the model & models
          if (!$group->id)
          {
               $group->id = $groupRecord->id;
          }

          return true;
     }
     else
     {
          $group->addErrors($groupRecord->getErrors());
          return false;
     }
}
$groupFieldGroupModel
Returnsbool

Saves a field group.

saveLayout() #

public function saveLayout(FieldLayoutModel $layout)
{
     // First save the layout
     $layoutRecord = new FieldLayoutRecord();
     $layoutRecord->type = $layout->type;
     $layoutRecord->save(false);
     $layout->id = $layoutRecord->id;

     foreach ($layout->getTabs() as $tab)
     {
          $tabRecord = new FieldLayoutTabRecord();
          $tabRecord->layoutId  = $layout->id;
          $tabRecord->name      = $tab->name;
          $tabRecord->sortOrder = $tab->sortOrder;
          $tabRecord->save(false);
          $tab->id = $tabRecord->id;

          foreach ($tab->getFields() as $field)
          {
               $fieldRecord = new FieldLayoutFieldRecord();
               $fieldRecord->layoutId  = $layout->id;
               $fieldRecord->tabId     = $tab->id;
               $fieldRecord->fieldId   = $field->fieldId;
               $fieldRecord->required  = $field->required;
               $fieldRecord->sortOrder = $field->sortOrder;
               $fieldRecord->save(false);
               $field->id = $fieldRecord->id;
          }
     }

     // Fire an 'onSaveFieldLayout' event
     $this->onSaveFieldLayout(new Event($this, array(
          'layout' => $layout,
     )));

     return true;
}
$layoutFieldLayoutModel
Returnsbool

Saves a field layout.

validateField() #

public function validateField(FieldModel $field)
{
     $fieldRecord = $this->_getFieldRecord($field);

     if (!$field->context)
     {
          $field->context = craft()->content->fieldContext;
     }

     $fieldRecord->groupId      = $field->groupId;
     $fieldRecord->name         = $field->name;
     $fieldRecord->handle       = $field->handle;
     $fieldRecord->context      = $field->context;
     $fieldRecord->instructions = $field->instructions;
     $fieldRecord->translatable = $field->translatable;
     $fieldRecord->type         = $field->type;

     // Get the field type
     $fieldType = $field->getFieldType();

     // Give the field type a chance to prep the settings from post
     $preppedSettings = $fieldType->prepSettings($field->settings);

     // Set the prepped settings on the FieldRecord and the field type
     $fieldRecord->settings = $preppedSettings;
     $fieldType->setSettings($preppedSettings);

     // Run validation
     $recordValidates = $fieldRecord->validate();
     $settingsValidate = $fieldType->getSettings()->validate();

     if ($recordValidates && $settingsValidate)
     {
          return true;
     }
     else
     {
          $field->addErrors($fieldRecord->getErrors());
          $field->addSettingErrors($fieldType->getSettings()->getErrors());
          return false;
     }
}
$fieldFieldModel
Returnsbool

Validates a field's settings.