You are here

function custom_pub_form_node_form_alter in Custom Publishing Options 8

Implements hook_form_BASE_FORM_ID_alter(). Regroup any custom publishing options to be under a grouped tab on the node form.

Parameters

$form:

FormStateInterface $form_state:

File

./custom_pub.module, line 85

Code

function custom_pub_form_node_form_alter(&$form, FormStateInterface $form_state) {
  $entities = \Drupal::entityTypeManager()
    ->getStorage('custom_publishing_option')
    ->loadMultiple();
  $form_keys = Element::children($form);
  $user = \Drupal::currentUser();
  $custom_publish_options = FALSE;
  foreach ($entities as $machine_name => $entity) {
    if ($entity
      ->isPublishUnderPromoteOptions()) {
      if (in_array($entity
        ->id(), $form_keys)) {
        $form[$entity
          ->id()]['#group'] = 'options';
        $form[$entity
          ->id()]['#access'] = $user
          ->hasPermission('can set node publish state to ' . $entity
          ->id());
        $custom_publish_options = TRUE;
      }
    }
    else {
      if (in_array($entity
        ->id(), $form_keys)) {
        $form[$entity
          ->id()]['#group'] = 'custom_publish_options';
        $form[$entity
          ->id()]['#access'] = $user
          ->hasPermission('can set node publish state to ' . $entity
          ->id());
        $custom_publish_options = TRUE;
      }
    }
  }

  // show the fieldset if we have options the user can use.
  if ($custom_publish_options) {
    $form['custom_publish_options'] = [
      '#type' => 'details',
      '#title' => t('Custom Publish Options'),
      '#group' => 'advanced',
      '#attributes' => [
        'class' => [
          'node-form-custom-publish-options',
        ],
      ],
      '#weight' => 100,
      '#optional' => TRUE,
    ];
  }
}