You are here

function flag_lists_form_alter in Flag Lists 4.0.x

Same name and namespace in other branches
  1. 8 flag_lists.module \flag_lists_form_alter()
  2. 6 flag_lists.module \flag_lists_form_alter()
  3. 7.3 flag_lists.module \flag_lists_form_alter()
  4. 7 flag_lists.module \flag_lists_form_alter()

Implements hook_form_alter().

File

./flag_lists.module, line 152
Contains flag_lists.module.

Code

function flag_lists_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $flagListsService = \Drupal::service('flaglists');

  // Change form id here.
  if (in_array($form_id, [
    'flag_add_form',
    'flag_edit_form',
  ])) {
    $form['fl_fieldset'] = [
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => t('Flagging Collection Template'),
      '#description' => t('Flagging Collections are lists / collections of flags grouped together. By marking this flag as a <em>Flagging Collection Template</em> the flag will be made available as a template flag for the Flag lists module. The settings above will then affect how the <em>Flagging Collection link</em> will be presented on each entity. This is just like the normal flag functionality.'),
      '#tree' => FALSE,
      '#weight' => 60,
      '#prefix' => '<div id="link-type-settings-wrapper">',
      '#suffix' => '</div>',
    ];

    // Check if this flag already is a Flag list Template
    // i.e. has a FlagForList.
    $isFlagForList = FALSE;
    if (!empty($form['#flag']
      ->id())) {
      $flagListsService = \Drupal::service('flaglists');
      $flag_template = $flagListsService
        ->getFlagForListById($form['#flag']
        ->id());
      $isFlagForList = !empty($flag_template);
    }
    $form['fl_fieldset']['flag_lists_flag'] = [
      '#type' => 'checkbox',
      '#title' => t('Flagging Collection Template'),
      '#description' => t('Will this flag be used as a template flag for Flagging Collections?'),
      '#default_value' => $isFlagForList,
      '#disabled' => $isFlagForList,
      '#weight' => '0',
    ];
    $form['actions']['submit']['#submit'][] = 'flag_lists_save_submit';
  }
  elseif ($form_id == 'flag_add_page') {

    // Remove our own entities in order to avoid recursion.
    unset($form['flag_entity_type']['#options']['entity:flag_list_item']);
    unset($form['flag_entity_type']['#options']['entity:flagging_collection']);
  }
}