You are here

class FormAssemblyEntityController in FormAssembly 7

@file Provides a controller building upon the Entity API but with some additional display and search.

Longer file description goes here Author: Shawn P. Duncan Date: 7/23/14 Time: 4:18 PM

Hierarchy

Expanded class hierarchy of FormAssemblyEntityController

1 string reference to 'FormAssemblyEntityController'
formassembly_entity_info in ./formassembly.module
Implements hook_entity_info().

File

includes/FormAssemblyEntityController.php, line 13
Provides a controller building upon the Entity API but with some additional display and search.

View source
class FormAssemblyEntityController extends EntityAPIController {

  /**
   * Extends the parent method to add FormAssembly markup.
   *
   * @param FormAssemblyEntity $entity
   *   The fa_form entity being rendered
   *
   * @param string $view_mode
   *   The view mode being displayed
   *
   * @param null $langcode
   *   optional
   *
   * @param array $content
   *   optional
   *
   * @return array
   *   A Drupal render array
   */
  public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
    $client_id = variable_get('formassembly_oauth_cid', '');
    $client_secret = variable_get('formassembly_oauth_secret', '');
    $request = new FormAssemblyRequest($client_id, $client_secret);
    $token = $request
      ->getToken();
    if ($token) {
      if (empty($_GET['tfa_next'])) {
        $markup = $request
          ->getFormMarkup($entity);
      }
      else {
        $markup = $request
          ->getNextForm($_GET['tfa_next']);
      }

      // Split the HTML markup into that belonging in the Body and that
      // belonging in the Head.
      list($head_markup, $body_markup) = $request
        ->splitHTML($markup);
      if (!empty($head_markup)) {
        $head_markup_render_array = array(
          '#type' => 'markup',
          '#markup' => $head_markup,
        );
        drupal_add_html_head($head_markup_render_array, 'formassembly_head');
      }
      $content['fa_markup'] = array(
        '#type' => 'markup',
        '#markup' => $body_markup,
      );
      drupal_page_is_cacheable(FALSE);
    }
    else {
      watchdog('formassembly', 'Could not get markup for form with faid: @faid.', array(
        '@faid' => $entity->faid,
      ), WATCHDOG_ERROR);
    }
    return parent::buildContent($entity, $view_mode, $langcode, $content);
  }

  /**
   * Selects entities to load by property.
   *
   * Adapted from a patch to
   * EntityStorageControllerInterface::loadByProperties()
   * Patch posted at https://www.drupal.org/files/1184272-77.patch
   *
   * @param array $values
   *   An array of property values.
   *
   *   The array key is the property name and array value is the value of that
   *   property to be matched. The database operator will set to '='
   *   for single values and to 'IN' for an array of values.
   */
  public function loadByProperties($values = array()) {

    // Build a query to fetch the entity IDs.
    $entity_query = new EntityFieldQuery();
    $entity_query
      ->entityCondition('entity_type', 'fa_form');
    $this
      ->buildPropertyQuery($entity_query, $values);
    $result = $entity_query
      ->execute();
    $entities = array();
    if (!empty($result['fa_form'])) {
      $entities = $this
        ->load(array_keys($result['fa_form']));
    }
    return $entities;
  }

  /**
   * Builds an entity query.
   *
   * Adpated from patch for EntityStorageControllerInterface::loadByProperties()
   * posted at https://www.drupal.org/files/1184272-77.patch
   *
   * @param 'Drupal\entity\EntityFieldQuery' $entity_query
   *   EntityFieldQuery instance.
   * @param array $values
   *   An associative array of properties of the entity, where the keys are the
   *   property names and the values are the values those properties must have.
   */
  protected function buildPropertyQuery(EntityFieldQuery $entity_query, array $values) {
    foreach ($values as $name => $value) {
      $entity_query
        ->propertyCondition($name, $value);
    }
  }

  /**
   * Returns the entity id and value of a property for all fa_form entities.
   *
   * @param string $property
   *   The name of the entity property.
   *
   * @return DatabaseStatementInterface|null
   *   If the property exists the query results are returned.
   */
  public function loadPropertySet($property) {
    $info = entity_get_property_info('fa_form');
    if (array_key_exists($property, $info['properties'])) {
      $query = db_select('formassembly', 'fa');
      $query
        ->fields('fa', array(
        'eid',
        $property,
      ));
      $stored = $query
        ->execute();
      return $stored;
    }
    else {
      watchdog('FormAssembly', 'Failed to load Property Set. @property: Not a valid property of fa_form', 'error', array(
        '@property' => $property,
      ), WATCHDOG_ERROR);
      return NULL;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::attachLoad protected function Attaches data to entities upon loading. 4
DrupalDefaultEntityController::cacheGet protected function Gets entities from the static cache. 1
DrupalDefaultEntityController::cacheSet protected function Stores entities in the static entity cache.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
EntityAPIController::$bundleKey protected property
EntityAPIController::$cacheComplete protected property
EntityAPIController::$defaultRevisionKey protected property
EntityAPIController::buildQuery protected function Overrides DrupalDefaultEntityController::buildQuery(). Overrides DrupalDefaultEntityController::buildQuery 1
EntityAPIController::create public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::create
EntityAPIController::delete public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::delete 1
EntityAPIController::deleteRevision public function Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface::deleteRevision
EntityAPIController::export public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::export 1
EntityAPIController::import public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::import
EntityAPIController::invoke public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::invoke 1
EntityAPIController::load public function Overridden. Overrides DrupalDefaultEntityController::load 1
EntityAPIController::query public function Builds and executes the query for loading.
EntityAPIController::renderEntityProperty protected function Renders a single entity property.
EntityAPIController::resetCache public function Overrides DrupalDefaultEntityController::resetCache(). Overrides DrupalDefaultEntityController::resetCache 1
EntityAPIController::save public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::save 1
EntityAPIController::saveRevision protected function Saves an entity revision.
EntityAPIController::view public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::view 1
EntityAPIController::__construct public function Overridden. Overrides DrupalDefaultEntityController::__construct 1
FormAssemblyEntityController::buildContent public function Extends the parent method to add FormAssembly markup. Overrides EntityAPIController::buildContent
FormAssemblyEntityController::buildPropertyQuery protected function Builds an entity query.
FormAssemblyEntityController::loadByProperties public function Selects entities to load by property.
FormAssemblyEntityController::loadPropertySet public function Returns the entity id and value of a property for all fa_form entities.