You are here

class EntityReferenceBehavior_EntityQueue in Entityqueue 7

Defines a Entityreference behavior handler for Entityqueue.

Hierarchy

Expanded class hierarchy of EntityReferenceBehavior_EntityQueue

1 string reference to 'EntityReferenceBehavior_EntityQueue'
entityqueue.inc in plugins/entityreference/behavior/entityqueue.inc

File

plugins/entityreference/behavior/EntityReferenceBehavior_EntityQueue.class.php, line 6

View source
class EntityReferenceBehavior_EntityQueue extends EntityReference_BehaviorHandler_Abstract {

  /**
   * Overrides EntityReference_BehaviorHandler_Abstract::validate().
   */
  public function validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
    if ($entity_type == 'entityqueue_subqueue') {
      $queue = entityqueue_queue_load($entity->queue);
      $min_size = $queue->settings['min_size'];
      $max_size = $queue->settings['max_size'];
      $act_as_queue = isset($queue->settings['act_as_queue']) ? $queue->settings['act_as_queue'] : 0;
      $eq_items = array_filter($items, function ($value) {
        return !empty($value["target_id"]) ? TRUE : FALSE;
      });
      if (count($eq_items) < $min_size && $entity->op != t('Add item')) {
        $errors[$field['field_name']][$langcode][0][] = array(
          'error' => 'entityqueue_min_size',
          'message' => t("The minimum number of items in this queue is @min_size.", array(
            '@min_size' => $min_size,
          )),
        );
      }
      elseif (!$act_as_queue && count($eq_items) > $max_size && $max_size > 0) {
        $errors[$field['field_name']][$langcode][count($items) - 1][] = array(
          'error' => 'entityqueue_max_size',
          'message' => t("The maximum number of items in this queue is @max_size.", array(
            '@max_size' => $max_size,
          )),
        );
      }
    }
  }

  /**
   * Overrides EntityReference_BehaviorHandler_Abstract::presave().
   */
  public function presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
    if ($entity_type == 'entityqueue_subqueue') {
      $queue = entityqueue_queue_load($entity->queue);
      $max_size = $queue->settings['max_size'];
      $act_as_queue = isset($queue->settings['act_as_queue']) ? $queue->settings['act_as_queue'] : 0;

      // Not all widgets can add to top, so we check if that option is set,
      // and default "bottom" as is normal for entity reference widgets.
      $add_position = isset($instance['widget']['settings']['add_position']) && $instance['widget']['settings']['add_position'] === 'top' ? 'top' : 'bottom';
      if ($act_as_queue) {
        $eq_items = array_filter($items, function ($value) {
          return !empty($value["target_id"]) ? TRUE : FALSE;
        });

        // Remove items exceeding the limit.
        if (count($eq_items) > $max_size && $max_size > 0) {

          // Remove from the end if items are added to the top.
          if ($add_position === 'top') {
            $items = array_slice($eq_items, 0, $max_size);
          }
          else {
            $items = array_slice($eq_items, -$max_size);
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReferenceBehavior_EntityQueue::presave public function Overrides EntityReference_BehaviorHandler_Abstract::presave(). Overrides EntityReference_BehaviorHandler_Abstract::presave
EntityReferenceBehavior_EntityQueue::validate public function Overrides EntityReference_BehaviorHandler_Abstract::validate(). Overrides EntityReference_BehaviorHandler_Abstract::validate
EntityReference_BehaviorHandler_Abstract::$behavior protected property The name of the behavior plugin.
EntityReference_BehaviorHandler_Abstract::$plugin protected property The plugin definition.
EntityReference_BehaviorHandler_Abstract::access public function Determine if handler should appear. Overrides EntityReference_BehaviorHandler::access 1
EntityReference_BehaviorHandler_Abstract::delete public function Act before deleting an entity with an entity reference field. Overrides EntityReference_BehaviorHandler::delete 2
EntityReference_BehaviorHandler_Abstract::entityPostDelete public function Act after deleting an entity. Overrides EntityReference_BehaviorHandler::entityPostDelete
EntityReference_BehaviorHandler_Abstract::entityPostInsert public function Act after inserting an entity. Overrides EntityReference_BehaviorHandler::entityPostInsert 1
EntityReference_BehaviorHandler_Abstract::entityPostUpdate public function Act after updating an entity. Overrides EntityReference_BehaviorHandler::entityPostUpdate 1
EntityReference_BehaviorHandler_Abstract::insert public function Act before inserting an entity reference field. Overrides EntityReference_BehaviorHandler::insert 2
EntityReference_BehaviorHandler_Abstract::is_empty_alter public function Alter the empty status of a field item. Overrides EntityReference_BehaviorHandler::is_empty_alter
EntityReference_BehaviorHandler_Abstract::load public function Act on loading entity reference fields of entities. Overrides EntityReference_BehaviorHandler::load 2
EntityReference_BehaviorHandler_Abstract::postDelete public function Act after deleting an entity with an entity reference field. Overrides EntityReference_BehaviorHandler::postDelete
EntityReference_BehaviorHandler_Abstract::postInsert public function Act after inserting an entity reference field. Overrides EntityReference_BehaviorHandler::postInsert
EntityReference_BehaviorHandler_Abstract::postUpdate public function Act after updating an entity reference field. Overrides EntityReference_BehaviorHandler::postUpdate
EntityReference_BehaviorHandler_Abstract::property_info_alter public function Alter the properties information of a field instance. Overrides EntityReference_BehaviorHandler::property_info_alter
EntityReference_BehaviorHandler_Abstract::schema_alter public function Alter the field schema. Overrides EntityReference_BehaviorHandler::schema_alter
EntityReference_BehaviorHandler_Abstract::settingsForm public function Generate a settings form for this handler. Overrides EntityReference_BehaviorHandler::settingsForm 4
EntityReference_BehaviorHandler_Abstract::update public function Act before updating an entity reference field. Overrides EntityReference_BehaviorHandler::update 2
EntityReference_BehaviorHandler_Abstract::views_data_alter public function Alter the views data of a field. Overrides EntityReference_BehaviorHandler::views_data_alter 1
EntityReference_BehaviorHandler_Abstract::__construct public function Constructor for the behavior. Overrides EntityReference_BehaviorHandler::__construct