You are here

class FlexiformFormEntityEntityReference in Flexiform 7

Form Entity Class for Managing the Entity Reference fields

Hierarchy

Expanded class hierarchy of FlexiformFormEntityEntityReference

1 string reference to 'FlexiformFormEntityEntityReference'
flexiform_flexiform_entity_getter_info in ./flexiform.flexiform.inc
Implements hook_flexiform_entity_getter_info().

File

includes/form_entity/entityreference.form_entity.inc, line 10
Contains class for a basic entity getter.

View source
class FlexiformFormEntityEntityReference extends FlexiformFormEntityBase {

  /**
   * {@inheritdoc}
   */
  public function getEntity() {
    parent::getEntity();
    $settings = $this->settings;

    // Get the base entity.
    $base_entity = $this
      ->getParam('base');

    // Get the delta.
    $delta = 0;
    if (!empty($settings['delta'])) {
      $delta = $settings['delta'];
    }

    // Check we have enough information to load the entity.
    if (!$base_entity || empty($base_entity->{$this->getter['field_name']}[LANGUAGE_NONE][$delta]['target_id'])) {
      return empty($base_entity) ? FALSE : $this
        ->createEntity();
    }

    // Load the entity;
    $target_id = $base_entity->{$this->getter['field_name']}[LANGUAGE_NONE][$delta]['target_id'];
    $target_entity = entity_load_single($this->entity_type, $target_id);
    if (!$target_entity) {
      return FALSE;
    }
    return $this
      ->checkBundle($target_entity) ? $target_entity : FALSE;
  }

  /**
   * Create an entity if the settings allow it.
   */
  protected function createEntity() {
    if (empty($this->settings['create'])) {
      return FALSE;
    }
    $entity_info = entity_get_info($this->entity_type);
    $values = array();
    if (!empty($entity_info['entity keys']['bundle'])) {
      $values[$entity_info['entity keys']['bundle']] = $this->settings['bundle'];
    }
    return entity_create($this->entity_type, $values);
  }

  /**
   * {@inheritdoc}
   */
  public function saveEntity($entity) {

    // If the entity is still false do not save it!
    if ($entity === FALSE) {
      return;
    }

    // Save the entity.
    entity_save($this->entity_type, $entity);
    list($id, , ) = entity_extract_ids($this->entity_type, $entity);

    // Get Settings for this Getter/Setter.
    $settings = $this->settings;

    // Get the delta.
    $delta = 0;
    if (!empty($settings['delta'])) {
      $delta = $settings['delta'];
    }

    // Get the base entity and save the reference back.
    $base_entity = $this
      ->getParam('base');
    if (empty($base_entity->{$this->getter['field_name']}[LANGUAGE_NONE][$delta]['target_id']) || $base_entity->{$this->getter['field_name']}[LANGUAGE_NONE][$delta]['target_id'] != $id) {
      $base_entity->{$this->getter['field_name']}[LANGUAGE_NONE][$delta]['target_id'] = $id;
      $base_entity_type = $this
        ->getParamType('base');
      entity_save($base_entity_type, $base_entity);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function configForm($form, &$form_state) {
    $form = parent::configForm($form, $form_state);
    $field = field_info_field($this->getter['field_name']);

    // Allow the selection of which one to use.
    if ($field['cardinality'] != 1) {
      $options = array();
      $i = 0;
      while ($i < 10) {
        if ($field['cardinality'] > 0 && $i >= $field['cardinality']) {
          break;
        }
        $options[$i] = $i + 1;
        ++$i;
      }
      $form['settings']['delta'] = array(
        '#title' => t('Delta'),
        '#description' => t('Select which value of this field you want to use.'),
        '#type' => 'select',
        '#options' => $options,
        '#default_value' => !empty($this->settings['delta']) ? $this->settings['delta'] : 0,
      );
    }
    $form['settings']['create'] = array(
      '#title' => t('Create if Empty'),
      '#description' => t('When ticked, if the field is empty, a new entity will be created.'),
      '#type' => 'checkbox',
      '#default_value' => !empty($this->settings['create']),
    );
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlexiformFormEntityBase::$entity_namespace public property The namespace of this entity.
FlexiformFormEntityBase::$entity_type public property The type of this entity.
FlexiformFormEntityBase::$getter public property Details of the getter.
FlexiformFormEntityBase::$manager public property The Flexiform Entity Manager
FlexiformFormEntityBase::$settings public property The settings for this entity on the flexiform.
FlexiformFormEntityBase::checkBundle public function Check bundle.
FlexiformFormEntityBase::configFormSubmit public function Submit the Configuration Form. Overrides FlexiformFormEntityInterface::configFormSubmit
FlexiformFormEntityBase::configFormValidate public function Validate the configuration form. Overrides FlexiformFormEntityInterface::configFormValidate
FlexiformFormEntityBase::getParam public function Get a Parameter From the Entity Manager.
FlexiformFormEntityBase::getParamSettings public function Get a Parameter's entity settings from the Entity Manager.
FlexiformFormEntityBase::getParamType public function Get the entity type of a parameter.
FlexiformFormEntityBase::__construct public function Construct a Flexiform Form Entity class.
FlexiformFormEntityEntityReference::configForm public function Get the Configuration Form. Overrides FlexiformFormEntityBase::configForm
FlexiformFormEntityEntityReference::createEntity protected function Create an entity if the settings allow it.
FlexiformFormEntityEntityReference::getEntity public function Get the entity for the form. Overrides FlexiformFormEntityBase::getEntity
FlexiformFormEntityEntityReference::saveEntity public function Save the entity upon submission of the form. Overrides FlexiformFormEntityBase::saveEntity