You are here

class EntityReference_SelectionHandler_EntityQueue in Entityqueue 7

Defines a Entityreference selection handler for Entityqueue.

Hierarchy

Expanded class hierarchy of EntityReference_SelectionHandler_EntityQueue

1 string reference to 'EntityReference_SelectionHandler_EntityQueue'
entityqueue.inc in plugins/entityreference/selection/entityqueue.inc

File

plugins/entityreference/selection/EntityReference_SelectionHandler_EntityQueue.class.php, line 11
Definition of EntityReference_SelectionHandler_EntityQueue.

View source
class EntityReference_SelectionHandler_EntityQueue extends EntityReference_SelectionHandler_Generic {

  /**
   * Overrides EntityReference_SelectionHandler_Generic::getInstance().
   */
  public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
    return new EntityReference_SelectionHandler_EntityQueue($field, $instance, $entity_type, $entity);
  }

  /**
   * Constructs the EntityQueue selection handler.
   */
  protected function __construct($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
    parent::__construct($field, $instance, $entity_type, $entity);
    $queue_name = NULL;
    if (!empty($entity->queue)) {
      $queue_name = $entity->queue;
    }
    elseif (!empty($instance['bundle'])) {
      $queue_name = $instance['bundle'];
    }
    if (!empty($queue_name)) {
      $this->queue = entityqueue_queue_load($queue_name);
    }

    // Override the entityreference settings with our own.
    $this->field['settings']['handler_settings']['target_bundles'] = NULL;
  }

  /**
   * Overrides EntityReference_SelectionHandler_Generic::settingsForm().
   */
  public static function settingsForm($field, $instance) {
    $form = parent::settingsForm($field, $instance);
    $access = $instance['entity_type'] === 'entityqueue_subqueue';

    // Show an explanation describing where bundles may be selected.
    $form['warning'] = array(
      '#type' => 'item',
      '#title' => t('DO NOT USE'),
      '#markup' => t('This mode should only be used on Entity Queues. This field is on a %type entity type and is not supported.', array(
        '%type' => $instance['entity_type'],
      )),
      '#weight' => -1,
      '#access' => !$access,
    );

    // Force all bundles to be accepted.
    $form['target_bundles'] = array(
      '#type' => 'value',
      '#value' => array(),
    );

    // Show an explanation describing where bundles may be selected.
    $form['target_bundles_help'] = array(
      '#type' => 'item',
      '#title' => t('Target bundles'),
      '#markup' => t('Bundles are filtered on a per-queue basis from the <a href="!url">queue\'s settings</a>.', array(
        '!url' => url('admin/structure/entityqueue/list/' . $instance['bundle'] . '/edit'),
      )),
      '#weight' => -1,
      '#access' => $access,
    );
    $form['sort']['#access'] = $access;
    return $form;
  }

  /**
   * Overrides EntityReference_SelectionHandler_Generic::buildEntityFieldQuery().
   */
  public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {

    // Ensure that the 'target_bundles' setting from the field is not used.
    $this->field['settings']['handler_settings']['target_bundles'] = NULL;
    $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance, $this->entity_type, $this->entity);
    $query = $handler
      ->buildEntityFieldQuery($match, $match_operator);
    if (!empty($this->queue->settings['target_bundles'])) {
      $query
        ->entityCondition('bundle', $this->queue->settings['target_bundles'], 'IN');
    }
    return $query;
  }

  /**
   * Implements EntityReferenceHandler::validateReferencableEntities().
   */
  public function validateReferencableEntities(array $ids) {
    $referencable = parent::validateReferencableEntities($ids);

    // Allow users to save the queue even if they don't have access to an
    // existing entity in the queue. See https://www.drupal.org/node/2383903
    $existing = $this
      ->getCurrentlyReferencedEntityIds();
    return array_unique(array_merge($referencable, $existing));
  }

  /**
   * Gets ids of existing entities in the queue.
   *
   * @return array
   *   Entity ids that are currently referenced by the entity.
   */
  public function getCurrentlyReferencedEntityIds() {
    $ret = array();
    if (isset($this->entity) && isset($this->field)) {
      $entity_type = $this->entity_type;
      $field_name = $this->field['field_name'];
      $wrapper = entity_metadata_wrapper($entity_type, $this->entity);
      $ret = $wrapper->{$field_name}
        ->raw();
    }
    return $ret;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReference_SelectionHandler_EntityQueue::buildEntityFieldQuery public function Overrides EntityReference_SelectionHandler_Generic::buildEntityFieldQuery(). Overrides EntityReference_SelectionHandler_Generic::buildEntityFieldQuery
EntityReference_SelectionHandler_EntityQueue::getCurrentlyReferencedEntityIds public function Gets ids of existing entities in the queue.
EntityReference_SelectionHandler_EntityQueue::getInstance public static function Overrides EntityReference_SelectionHandler_Generic::getInstance(). Overrides EntityReference_SelectionHandler_Generic::getInstance
EntityReference_SelectionHandler_EntityQueue::settingsForm public static function Overrides EntityReference_SelectionHandler_Generic::settingsForm(). Overrides EntityReference_SelectionHandler_Generic::settingsForm
EntityReference_SelectionHandler_EntityQueue::validateReferencableEntities public function Implements EntityReferenceHandler::validateReferencableEntities(). Overrides EntityReference_SelectionHandler_Generic::validateReferencableEntities
EntityReference_SelectionHandler_EntityQueue::__construct protected function Constructs the EntityQueue selection handler. Overrides EntityReference_SelectionHandler_Generic::__construct
EntityReference_SelectionHandler_Generic::countReferencableEntities public function Implements EntityReferenceHandler::countReferencableEntities(). Overrides EntityReference_SelectionHandler::countReferencableEntities
EntityReference_SelectionHandler_Generic::ensureBaseTable public function Ensure a base table exists for the query.
EntityReference_SelectionHandler_Generic::entityFieldQueryAlter public function Implements EntityReferenceHandler::entityFieldQueryAlter(). Overrides EntityReference_SelectionHandler::entityFieldQueryAlter 5
EntityReference_SelectionHandler_Generic::getLabel public function Implements EntityReferenceHandler::getLabel(). Overrides EntityReference_SelectionHandler::getLabel 1
EntityReference_SelectionHandler_Generic::getReferencableEntities public function Implements EntityReferenceHandler::getReferencableEntities(). Overrides EntityReference_SelectionHandler::getReferencableEntities 1
EntityReference_SelectionHandler_Generic::reAlterQuery protected function Helper method: pass a query to the alteration system again.
EntityReference_SelectionHandler_Generic::validateAutocompleteInput public function Implements EntityReferenceHandler::validateAutocompleteInput(). Overrides EntityReference_SelectionHandler::validateAutocompleteInput