You are here

function menu_token_form_submit in Menu Token 8

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

Custom form submit handler.

1 string reference to 'menu_token_form_submit'
menu_token_form_menu_link_content_form_alter in ./menu_token.module
Implements hook_form_FORM_ID_alter().

File

./menu_token.module, line 225
Contains menu_token.module.

Code

function menu_token_form_submit($form, &$form_state) {
  $values = $form_state
    ->getValues();
  $config_array = [
    "menu_token_enabled" => $values['menu_token_enabled'],
    "remove_if_replacement_is_not_present" => $values['remove_if_replacement_is_not_present'],
  ];
  $available_entities_configuration = \Drupal::config('menu_token.availableentitiesconfiguration');
  $data = $available_entities_configuration
    ->getRawData();
  foreach ($data['available_entities'] as $config_key => $config_item) {
    if ($config_item !== 0) {
      if (isset($values['menu_token_type_' . $config_item])) {
        if ($values['menu_token_type_' . $config_item] == 'user_defined') {
          if (empty($values[$config_item . 'custom_entity'])) {
            $values[$config_item . 'custom_entity'] = $_POST[$config_item . 'custom_entity'];
          }
          $config_array[$config_key] = [
            $values['menu_token_type_' . $config_item],
            $values[$config_item . 'custom_entity'],
          ];
        }
        else {
          $config_array[$config_key] = [
            $values['menu_token_type_' . $config_item],
            0,
          ];
        }
      }
    }
  }
  $form_entity = $form_state
    ->getFormObject()
    ->getEntity();
  $uuid = $form_entity
    ->get('uuid')->value;

  // Load the configuration if it exists.
  $config_menu = \Drupal::entityTypeManager()
    ->getStorage('link_configuration_storage')
    ->load($uuid);
  if ($values['menu_token_enabled'] == 0) {
    if (!empty($config_menu)) {
      $config_menu
        ->delete();
    }
  }
  else {
    $token_service = \Drupal::token();

    // Have to find out which type to use by tokens.
    // Available in link data and not in configuration entity.
    $title_tokens = $token_service
      ->scan($values['title'][0]['value']);
    $uri_tokens = $token_service
      ->scan($values['link'][0]['uri']);
    $merged = array_merge($title_tokens, $uri_tokens);
    $all_tokens = array_unique($merged, SORT_REGULAR);
    $all_token_types = [];
    foreach ($all_tokens as $k => $t) {
      $all_token_types[] = $k;
    }
    if (!empty($config_menu)) {
      $config_menu
        ->set("linkid", (string) $values['link'][0]['uri']);
      $config_menu
        ->set("configurationSerialized", serialize($config_array));
    }
    else {
      $config_menu = \Drupal::entityTypeManager()
        ->getStorage('link_configuration_storage')
        ->create([
        'id' => $uuid,
        'label' => 'Menu token link configuration',
        'linkid' => (string) $values['link'][0]['uri'],
        'configurationSerialized' => serialize($config_array),
      ]);
    }
    $config_menu
      ->save();
    $menu_token_menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    $menu_token_menu_link_manager
      ->rebuild();

    // Reset all static caches.
    drupal_static_reset();
    \Drupal::cache()
      ->deleteAll();

    // Flush asset file caches.
    \Drupal::service('asset.css.collection_optimizer')
      ->deleteAll();
    \Drupal::service('asset.js.collection_optimizer')
      ->deleteAll();
    _drupal_flush_css_js();

    // Wipe the Twig PHP Storage cache.
    PhpStorageFactory::get('twig')
      ->deleteAll();

    // Reset all static caches.
    drupal_static_reset();
  }
}