You are here

function social_tagging_form_alter in Open Social 8.4

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_tagging/social_tagging.module \social_tagging_form_alter()
  2. 8.5 modules/social_features/social_tagging/social_tagging.module \social_tagging_form_alter()
  3. 8.6 modules/social_features/social_tagging/social_tagging.module \social_tagging_form_alter()
  4. 8.7 modules/social_features/social_tagging/social_tagging.module \social_tagging_form_alter()
  5. 8.8 modules/social_features/social_tagging/social_tagging.module \social_tagging_form_alter()
  6. 10.3.x modules/social_features/social_tagging/social_tagging.module \social_tagging_form_alter()
  7. 10.0.x modules/social_features/social_tagging/social_tagging.module \social_tagging_form_alter()
  8. 10.1.x modules/social_features/social_tagging/social_tagging.module \social_tagging_form_alter()
  9. 10.2.x modules/social_features/social_tagging/social_tagging.module \social_tagging_form_alter()

Implements hook_form_alter().

File

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

Code

function social_tagging_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Act if the form has the tagging field.
  if (isset($form['social_tagging'])) {

    // Load tag config..
    $tag_settings = \Drupal::getContainer()
      ->get('config.factory')
      ->getEditable('social_tagging.settings');

    /** @var \Drupal\Core\Entity\Entity $form_entity */
    $form_entity = $form_state
      ->getFormObject()
      ->getEntity();

    // Switch for the entity_type.
    switch ($form_entity
      ->getEntityTypeId()) {
      case 'node':
        $type = $form_entity->type->entity
          ->id();
        $tag_enabled = $tag_settings
          ->get('tag_node_type_' . $type);
        break;
      case 'group':
        $type = $form_entity
          ->getEntityTypeId();
        $tag_enabled = $tag_settings
          ->get('tag_type_' . $type);
        break;
      default:
        $tag_enabled = FALSE;
        break;
    }

    // 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() || $tag_enabled == 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_entity);

        // 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_entity_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';
      }
    }
  }
}