You are here

class FlexiformFormEntityBase in Flexiform 7

Default Form Entity Class for Managing a form Entity.

Hierarchy

Expanded class hierarchy of FlexiformFormEntityBase

File

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

View source
class FlexiformFormEntityBase implements FlexiformFormEntityInterface {

  /**
   * The Flexiform Entity Manager
   * @var FlexiformFormEntityManagerInterface
   */
  public $manager;

  /**
   * The namespace of this entity.
   * @var string
   */
  public $entity_namespace;

  /**
   * The type of this entity.
   * @var string
   */
  public $entity_type;

  /**
   * Details of the getter.
   * @var array
   */
  public $getter = array();

  /**
   * The settings for this entity on the flexiform.
   * @var array
   */
  public $settings = array();

  /**
   * Construct a Flexiform Form Entity class.
   *
   * @param FlexiformFormEntityManagerInterface $manager
   * @param string $namespace
   * @param array $getter
   * @param array $settings (Optional)
   */
  public function __construct(FlexiformFormEntityManagerInterface $manager, $namespace, $getter, $settings = array()) {
    $this->manager = $manager;
    $this->entity_namespace = $namespace;
    $this->getter = $getter;
    $this->settings = $this->manager
      ->getEntitySettings($namespace) ?: $settings;
    $this->entity_type = $this->settings['entity_type'];
  }

  /**
   * Get a Parameter From the Entity Manager.
   *
   * @return
   *  An entity object.
   */
  public function getParam($param_name) {
    return $this->manager
      ->getEntity($this->settings['parameters'][$param_name]);
  }

  /**
   * Get a Parameter's entity settings from the Entity Manager.
   */
  public function getParamSettings($param_name) {
    return $this->manager
      ->getEntitySettings($this->settings['parameters'][$param_name]);
  }

  /**
   * Get the entity type of a parameter.
   */
  public function getParamType($param_name) {
    return $this->manager
      ->getEntityType($this->settings['parameters'][$param_name]);
  }

  /**
   * Check bundle.
   *
   * @param $entity
   *   The entity to check.
   *
   * @return boolean
   *   True if the entity is the right bundle for this form entity. False
   *   otherwise.
   */
  public function checkBundle($entity) {
    list(, , $bundle) = entity_extract_ids($this->entity_type, $entity);
    if ($bundle == $this->settings['bundle']) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntity() {
    if (isset($this->settings['save_on_submit']) && empty($this->settings['save_on_submit'])) {
      $this->manager
        ->skipOnSave($this->entity_namespace, TRUE);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function saveEntity($entity) {
    entity_save($this->entity_type, $entity);
  }

  /**
   * {@inheritdoc}
   */
  public function configForm($form, &$form_state) {
    $form['settings'] = array(
      '#type' => 'container',
      '#tree' => TRUE,
    );
    $form['settings']['save_on_submit'] = array(
      '#type' => 'checkbox',
      '#title' => t('Save Entity on Submit'),
      '#description' => t('Should this entity always be saved on submit?'),
      '#default_value' => isset($this->settings['save_on_submit']) ? $this->settings['save_on_submit'] : TRUE,
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save Settings'),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function configFormValidate($form, &$form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function configFormSubmit($form, &$form_state) {
    $this->settings = $form_state['values']['settings'] + $this->settings;
    $this->manager
      ->updateEntitySettings($this->entity_namespace, $this->settings);
  }

}

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::configForm public function Get the Configuration Form. Overrides FlexiformFormEntityInterface::configForm 3
FlexiformFormEntityBase::configFormSubmit public function Submit the Configuration Form. Overrides FlexiformFormEntityInterface::configFormSubmit
FlexiformFormEntityBase::configFormValidate public function Validate the configuration form. Overrides FlexiformFormEntityInterface::configFormValidate
FlexiformFormEntityBase::getEntity public function Get the entity for the form. Overrides FlexiformFormEntityInterface::getEntity 12
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::saveEntity public function Save the entity upon submission of the form. Overrides FlexiformFormEntityInterface::saveEntity 5
FlexiformFormEntityBase::__construct public function Construct a Flexiform Form Entity class.