You are here

function entityqueue_form_widget_form_node_form_alter in Entityqueue Form Widget 8

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

Implements hook_form_node_form_alter().

File

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

Code

function entityqueue_form_widget_form_node_form_alter(&$form, FormStateInterface $form_state) {
  $node = $form_state
    ->getFormObject()
    ->getEntity();
  $entity_id = $node
    ->id();

  // Works with entityqueue module version 8.x-1.0-alpha6 .
  $allowed_entityqueues = entity_qget_allowed_subque_list($node);

  // Check if there is any entityqueues to show/not show the widget.
  if (!empty($allowed_entityqueues)) {
    $url = Url::fromRoute('entity.entity_queue.collection');
    $form['entityqueue_form_widget'] = [
      '#type' => 'details',
      '#title' => t('Entityqueues settings'),
      '#group' => 'advanced',
      '#tree' => TRUE,
      '#weight' => 100,
      '#markup' => '<p>' . t('Choose from the available entityqueues below to push this content to. To reorder and manage each queue, please visit the @entityqueue_management_page', [
        '@entityqueue_management_page' => Link::fromTextAndUrl(t('Entityqueue management page'), $url)
          ->toString(),
      ]) . '</p>',
    ];
    $form['entityqueue_form_widget']['entityqueues'] = [];
    foreach ($allowed_entityqueues as $allowed_entityqueue_group) {
      foreach ($allowed_entityqueue_group as $allowed_entityqueue) {
        if (\Drupal::currentUser()
          ->hasPermission('update ' . $allowed_entityqueue . ' entityqueue') || \Drupal::currentUser()
          ->hasPermission('manipulate all entityqueues')) {
          $form['entityqueue_form_widget']['entityqueues'][$allowed_entityqueue] = _prepare_checkbox($entity_id, $allowed_entityqueue);
        }
      }
    }

    // Calling submit handler.
    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'entityqueue_form_widget_form_node_form_submit';
      }
    }
  }
}