You are here

public function EntityReferenceBehavior_EntityQueue::validate in Entityqueue 7

Overrides EntityReference_BehaviorHandler_Abstract::validate().

Overrides EntityReference_BehaviorHandler_Abstract::validate

File

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

Class

EntityReferenceBehavior_EntityQueue
Defines a Entityreference behavior handler for Entityqueue.

Code

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