You are here

function flag_form_alter in Flag 8.4

Same name and namespace in other branches
  1. 5 flag.module \flag_form_alter()
  2. 6.2 flag.module \flag_form_alter()
  3. 6 flag.module \flag_form_alter()
  4. 7.2 flag.module \flag_form_alter()

Implements hook_form_alter().

File

./flag.module, line 121
The Flag module.

Code

function flag_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $object = $form_state
    ->getFormObject();

  // We only want to operate on content entity forms.
  if (!$object instanceof ContentEntityFormInterface || $object instanceof ConfirmFormInterface) {
    return;
  }

  // Get the flags for the entity being edited by the form.
  $flag_service = \Drupal::service('flag');
  $entity = $object
    ->getEntity();
  $flags = $flag_service
    ->getAllFlags($entity
    ->getEntityTypeId(), $entity
    ->bundle());

  // Check the first flag and return early if the form isn't considered to be
  // an edit form.
  if (!empty($flags) && isset($flags[0]) && $flags[0] instanceof FlagInterface) {
    if (!$flags[0]
      ->getFlagTypePlugin()
      ->isAddEditForm($object
      ->getOperation())) {
      return;
    }
  }

  // Filter the flags to those that apply here:
  //  - the flag uses the entity type plugin.
  //  - the plugin is configured to output the flag in the entity form.
  //  - the current user has access to the flag.
  $filtered_flags = array_filter($flags, function (FlagInterface $flag) use ($object) {
    $plugin = $flag
      ->getFlagTypePlugin();
    $entity = $object
      ->getEntity();
    $action = $flag
      ->isFlagged($entity) ? 'unflag' : 'flag';
    $access = $flag
      ->actionAccess($action, NULL, $entity);
    return $plugin instanceof EntityFlagType && $plugin
      ->showOnForm() && $access
      ->isAllowed();
  });

  // If we still have any flags...
  if (!empty($filtered_flags)) {

    // Add a container to the form.
    $form['flag'] = [
      '#type' => 'details',
      '#title' => t('Flags'),
      '#attached' => [
        'library' => [
          'flag/flag.admin',
        ],
      ],
      '#group' => 'advanced',
      '#tree' => TRUE,
    ];

    /** @var FlagInterface $flag */
    foreach ($filtered_flags as $flag) {

      // Add each flag to the form.
      $form['flag'][$flag
        ->id()] = [
        '#type' => 'checkbox',
        '#title' => $flag
          ->label(),
        '#description' => $flag
          ->getLongText('flag'),
        '#default_value' => $flag
          ->isFlagged($entity) ? 1 : 0,
        '#return_value' => 1,
        // Used by our drupalSetSummary() on vertical tabs.
        '#attributes' => [
          'title' => $flag
            ->label(),
        ],
      ];
    }
    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'flag_form_submit';
      }
    }
  }
}