You are here

function menu_token_form_menu_link_content_form_alter in Menu Token 8

Same name and namespace in other branches
  1. 9.1.x menu_token.module \menu_token_form_menu_link_content_form_alter()

Implements hook_form_FORM_ID_alter().

File

./menu_token.module, line 36
Contains menu_token.module.

Code

function menu_token_form_menu_link_content_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form_state
    ->setCached(FALSE);
  $rebuild_with_custom_element = $form_state
    ->get('rebuild_with_custom_element');

  // Get the entity from the form state.
  $form_entity = $form_state
    ->getFormObject()
    ->getEntity();
  $link_id_from_entity = $form_entity
    ->get('link')->uri;
  $uuid = $form_entity
    ->get('uuid')->value;
  $available_entities_configuration = \Drupal::config('menu_token.availableentitiesconfiguration');
  $data = $available_entities_configuration
    ->getRawData();
  $config_array = [];
  if (!empty($link_id_from_entity)) {
    $config_menu = \Drupal::entityTypeManager()
      ->getStorage('link_configuration_storage')
      ->load($uuid);
    if (!empty($config_menu)) {
      $config_array = unserialize($config_menu
        ->get('configurationSerialized'));
    }
    else {
      $config_array = [
        "menu_token_enabled" => 0,
        "remove_if_replacement_is_not_present" => 0,
      ];
    }
  }
  if (!empty($form['link']['weight'])) {
    $link_weight = $form['link']['weight'];
  }
  else {
    $link_weight = -2;
  }
  if (empty($config_array['menu_token_enabled'])) {
    $config_array['menu_token_enabled'] = 0;
  }
  if (empty($config_array['remove_if_replacement_is_not_present'])) {
    $config_array['remove_if_replacement_is_not_present'] = 0;
  }
  $form['menu_token_enabled'] = [
    '#type' => 'checkbox',
    '#title' => t('<strong>Use tokens</strong> in title and in path.'),
    '#description' => t('Activate this option in order to use Menu token.'),
    '#default_value' => $config_array['menu_token_enabled'],
    '#weight' => $link_weight,
  ];
  $form['menu_token_options'] = [
    '#type' => 'fieldset',
    '#title' => t('Menu Token options'),
    '#collapsible' => TRUE,
    '#weight' => $link_weight,
    '#states' => [
      'visible' => [
        ':input[name="menu_token_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['menu_token_options']['menu_token_modal_link'] = [
    '#title' => t('Browse available tokens.'),
    '#type' => 'link',
    '#url' => Url::fromRoute('menu_token.page.show.available.tokens'),
    '#attributes' => [
      'class' => [
        'use-ajax',
      ],
      'data-dialog-type' => 'modal',
      'data-dialog-options' => Json::encode([
        'width' => 600,
        'height' => 600,
        'resizable' => TRUE,
      ]),
    ],
    '#states' => [
      'visible' => [
        ':input[name="menu_token_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // If entities exist and item is enabled.
  if (!empty($data['available_entities'])) {
    foreach ($data['available_entities'] as $config_key => $config_item) {
      if ($config_item !== 0) {
        $default_value_array = [
          "none",
        ];
        if (!empty($config_array[$config_item])) {
          $default_value_array = [
            $config_array[$config_item],
          ];
        }
        $default_selection = $default_value_array[0][0];
        $form['menu_token_options'][$config_item]['menu_token_type_' . $config_item] = [
          '#type' => 'select',
          '#title' => t("Method for %var", [
            '%var' => ucfirst($config_item),
          ]),
          '#options' => [
            'none' => t("Disabled", [
              '%var' => 'none',
            ]),
            'context' => t("Context from %var", [
              '%var' => ucfirst($config_item),
            ]),
            'random' => t("Random %var", [
              '%var' => ucfirst($config_item),
            ]),
            'user_defined' => t("User defined %var", [
              '%var' => ucfirst($config_item),
            ]),
          ],
          '#default_value' => $default_selection,
          '#executes_submit_callback' => TRUE,
          '#submit' => [
            'menu_token_custom_entity_submit',
          ],
          '#ajax' => [
            'callback' => 'menu_token_custom_entity_callback',
            'wrapper' => $config_item . '_custom_entity_container',
          ],
        ];

        // Define the container.
        $form['menu_token_options'][$config_item]['custom_entity_wrapper'] = [
          '#type' => 'container',
          '#attributes' => [
            'id' => $config_item . '_custom_entity_container',
          ],
        ];

        /* If it is rebuild from ajax */
        if (!empty($rebuild_with_custom_element)) {

          // Made new form element insert.
          if ($rebuild_with_custom_element == 'user_defined') {
            $form['menu_token_options'][$config_item]['custom_entity_wrapper'][$config_item . 'custom_entity'] = [
              '#title' => t('Entity ID'),
              '#description' => t('The id of the entity that this token handler should load.'),
              '#type' => 'textfield',
              '#default_value' => 1,
            ];
          }
        }
        else {

          // Build only if you have in config variable.
          if ($default_selection == 'user_defined') {
            $form['menu_token_options'][$config_item]['custom_entity_wrapper'][$config_item . 'custom_entity'] = [
              '#title' => t('Entity ID'),
              '#description' => t('The id of the entity that this token handler should load.'),
              '#type' => 'textfield',
              '#default_value' => $default_value_array[0][1],
            ];
          }
        }
      }
    }
  }
  $form['menu_token_options']['remove_if_replacement_is_not_present'] = [
    '#type' => 'checkbox',
    '#title' => t('Remove token if replacement is not present'),
    '#description' => t('If the replacement token is not available on the page being viewed, the token will be removed if checked.'),
    '#default_value' => $config_array['remove_if_replacement_is_not_present'],
  ];

  // Submit handler.
  $form['actions']['submit']['#submit'][] = 'menu_token_form_submit';
}