You are here

function _prepare_checkbox in Entityqueue Form Widget 8

Same name and namespace in other branches
  1. 2.0.x entityqueue_form_widget.module \_prepare_checkbox()

Prepare a checkbox for allowed entityqueue.

Parameters

int $entity_id: Entity ID.

string $allowed_entityqueue: Name of the allowed entityqueue.

Return value

array Values needed to render a checkbox.

1 call to _prepare_checkbox()
entityqueue_form_widget_form_node_form_alter in ./entityqueue_form_widget.module
Implements hook_form_node_form_alter().

File

./entityqueue_form_widget.module, line 161
Allows editors to add content to an entityqueue from the same add/edit form.

Code

function _prepare_checkbox($entity_id, $allowed_entityqueue) {

  // Get all checked queues for this entity.
  $query_checked_queues = \Drupal::database()
    ->select('entity_subqueue__items')
    ->distinct()
    ->condition('items_target_id', $entity_id);
  $query_checked_queues
    ->addField('entity_subqueue__items', 'entity_id');
  $result_checked_queues = array_keys($query_checked_queues
    ->execute()
    ->fetchAllAssoc('entity_id'));
  $entity_subqueue = \Drupal::entityTypeManager()
    ->getStorage('entity_subqueue')
    ->load($allowed_entityqueue);
  $number_of_items = count($entity_subqueue
    ->get('items')
    ->getValue());
  $queue_details = $entity_subqueue
    ->getQueue();
  $max_size = $queue_details
    ->getMaximumSize();
  $max_size = $max_size == '0' ? 'unlimited' : $max_size;
  $checked_flag = 0;
  if (in_array($allowed_entityqueue, $result_checked_queues)) {
    $checked_flag = 1;
  }
  $queue = $entity_subqueue
    ->getQueue();
  if ($queue
    ->getHandler() == 'multiple') {
    $title = $entity_subqueue
      ->label() . ' (' . $queue
      ->label() . ')';
  }
  else {
    $title = $entity_subqueue
      ->label();
  }
  return [
    '#type' => 'checkbox',
    '#title' => t('@queue_title <i>(@number_of_items out of @$max_size items)</i>', [
      '@queue_title' => $title,
      '@number_of_items' => $number_of_items,
      '@$max_size' => $max_size,
    ]),
    '#default_value' => $checked_flag,
    '#id' => $allowed_entityqueue,
  ];
}