You are here

public function EntityReferenceBehavior_EntityQueue::presave in Entityqueue 7

Overrides EntityReference_BehaviorHandler_Abstract::presave().

Overrides EntityReference_BehaviorHandler_Abstract::presave

File

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

Class

EntityReferenceBehavior_EntityQueue
Defines a Entityreference behavior handler for Entityqueue.

Code

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);
        }
      }
    }
  }
}