You are here

function social_tagging_form_node_form_alter in Open Social 8.3

Same name and namespace in other branches
  1. 8 modules/social_features/social_tagging/social_tagging.module \social_tagging_form_node_form_alter()
  2. 8.2 modules/social_features/social_tagging/social_tagging.module \social_tagging_form_node_form_alter()

Implements hook_form_FORM_ID_alter().

File

modules/social_features/social_tagging/social_tagging.module, line 107
Contains social_tagging.module.

Code

function social_tagging_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $node = $form_state
    ->getFormObject()
    ->getEntity();
  $node_type_tagging = TRUE;
  if ($node_type = $node->type->entity
    ->id()) {
    $config = \Drupal::getContainer()
      ->get('config.factory')
      ->getEditable('social_tagging.settings');
    $config_value = $config
      ->get('tag_node_type_' . $node_type);
    if ($config_value === FALSE || $config_value === 0) {
      $node_type_tagging = FALSE;
    }
  }

  // Get the taggingservice.
  $tag_service = Drupal::getContainer()
    ->get('social_tagging.tag_service');

  // Check if tagging is turned on/off.
  if (!$tag_service
    ->active() || !$tag_service
    ->hasContent() || $node_type_tagging === FALSE) {

    // Remove the field from the form.
    $form['social_tagging']['#access'] = FALSE;
    return;
  }
  if (isset($form['social_tagging'])) {

    // Add an extra vertical tab.
    $form['tagging'] = [
      '#type' => 'fieldset',
      '#title' => t('Tag content'),
      '#description' => '',
      '#group' => 'group_tagging',
      '#open' => TRUE,
      '#weight' => 50,
    ];
    if ($tag_service
      ->allowSplit()) {

      // Get the default value.
      $default_value = _social_tagging_node_form_defaults_values($form_state
        ->getFormObject()
        ->getEntity());

      // Get the main categories.
      $categories = $tag_service
        ->getCategories();

      // Loop over the categories.
      foreach ($categories as $tid => $category) {
        $field_name = 'social_tagging_' . social_tagging_to_machine_name($category);

        // Get the corresponding items.
        $options = $tag_service
          ->getChildren($tid);

        // Only add a field if the category has any options.
        if (count($options) > 0) {

          // Add a field.
          $form['tagging'][$field_name] = [
            '#type' => 'select',
            '#title' => $category,
            '#multiple' => TRUE,
            '#default_value' => $default_value,
            '#options' => $options,
            '#group' => 'group_tagging',
          ];
        }
      }

      // Deny access the social_tagging field altogether.
      $form['social_tagging']['#access'] = FALSE;

      // Add a custom submithandler.
      $form['#validate'][] = '_social_tagging_node_form_validate';
    }
    else {
      $options = [];
      foreach ($tag_service
        ->getCategories() as $key => $value) {
        $options[$value] = $tag_service
          ->getChildren($key);
      }
      $form['social_tagging']['widget']['#options'] = $options;

      // Move the social_tagging field in the group.
      $form['tagging']['social_tagging'] = $form['social_tagging'];
      unset($form['social_tagging']);
      $form['tagging']['social_tagging']['#group'] = 'group_tagging';
    }
  }
}