You are here

function flag_lists_form_alter in Flag Lists 8

Same name and namespace in other branches
  1. 6 flag_lists.module \flag_lists_form_alter()
  2. 7.3 flag_lists.module \flag_lists_form_alter()
  3. 7 flag_lists.module \flag_lists_form_alter()
  4. 4.0.x 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) {

  // Change form id here.
  if (in_array($form_id, [
    'flag_add_form',
    'flag_edit_form',
  ])) {
    $form['fl_fieldset'] = [
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => t('Flag list Template'),
      '#description' => t('Flag lists are lists / collections of flags grouped together. By marking this flag as a <em>Flag List 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>Flag list 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('Flag List Template'),
      '#description' => t('Will this flag be used as a template flag for Flag lists?'),
      '#default_value' => $isFlagForList,
      '#disabled' => $isFlagForList,
      '#weight' => '0',
    ];
    $form['actions']['submit']['#submit'][] = 'flag_lists_save_submit';
  }
  elseif ($form_id == 'user_admin_permissions') {

    // Remove the possible long list of unused flag permissions
    // due to the flag lists module.
    $flagListsService = Drupal::service('flaglists');
    $flagLists = $flagListsService
      ->getAllFlaggingCollections();
    foreach ($flagLists as $flagList) {
      $flag = 'flag ' . $flagList
        ->getRelatedFlag()
        ->id();
      $unflag = 'unflag ' . $flagList
        ->getRelatedFlag()
        ->id();
      unset($form['permissions'][$flag]);
      unset($form['permissions'][$unflag]);
    }

    // Check for access for the used template as well.
    $flagTemplates = $flagListsService
      ->getAllFlagForList();
    foreach ($flagTemplates as $flagTemplate) {

      // Do we really want to remove the templates from
      // the possibility of setting them separately?
      $flag = 'flag ' . $flagTemplate
        ->id();
      $unflag = 'unflag ' . $flagTemplate
        ->id();
      unset($form['permissions'][$flag]);
      unset($form['permissions'][$unflag]);
    }
  }
  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']);
  }
}