You are here

function social_tagging_social_tagging_field_form_alter in Open Social 8.9

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_tagging/social_tagging.module \social_tagging_social_tagging_field_form_alter()
  2. 10.0.x modules/social_features/social_tagging/social_tagging.module \social_tagging_social_tagging_field_form_alter()
  3. 10.1.x modules/social_features/social_tagging/social_tagging.module \social_tagging_social_tagging_field_form_alter()
  4. 10.2.x modules/social_features/social_tagging/social_tagging.module \social_tagging_social_tagging_field_form_alter()

Function do a changes related to social_tagging field.

Parameters

array $form: Form array.

\Drupal\Core\Form\FormStateInterface $form_state: Form State object.

\Drupal\Core\Entity\EntityInterface $form_entity: Entity object.

2 calls to social_tagging_social_tagging_field_form_alter()
social_tagging_form_alter in modules/social_features/social_tagging/social_tagging.module
Implements hook_form_alter().
social_tagging_inline_entity_form_entity_form_alter in modules/social_features/social_tagging/social_tagging.module
Implements hook_inline_entity_form_entity_form_alter().

File

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

Code

function social_tagging_social_tagging_field_form_alter(array &$form, FormStateInterface &$form_state, EntityInterface $form_entity) {
  if (!empty($form['social_tagging'])) {

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

    // Switch for the entity_type.
    switch ($form_entity
      ->getEntityTypeId()) {
      case 'node':
        $type = $form_entity
          ->bundle();
        $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' => 'select2',
              '#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';
      }
    }
  }
}