You are here

function token_node_menu_link_submit in Token 8

Entity builder.

1 string reference to 'token_node_menu_link_submit'
token_form_node_form_alter in ./token.module
Implements hook_form_BASE_FORM_ID_alter() for node_form.

File

./token.module, line 692
Enhances the token API in core: adds a browseable UI, missing tokens, etc.

Code

function token_node_menu_link_submit($entity_type, NodeInterface $node, &$form, FormStateInterface $form_state) {

  // Entity builders run twice, once during validation and again during
  // submission, so we only run this code after validation has been performed.
  if (!$form_state
    ->isValueEmpty('menu') && $form_state
    ->getTemporaryValue('entity_validated')) {
    $values = $form_state
      ->getValue('menu');
    if (!empty($values['enabled']) && trim($values['title'])) {
      if (!empty($values['menu_parent'])) {
        list($menu_name, $parent) = explode(':', $values['menu_parent'], 2);
        $values['menu_name'] = $menu_name;
        $values['parent'] = $parent;
      }

      // Construct an unsaved entity.
      if ($entity_id = $form_state
        ->getValue([
        'menu',
        'entity_id',
      ])) {

        // Use the existing menu_link_content entity.
        $entity = MenuLinkContent::load($entity_id);

        // If the loaded MenuLinkContent doesn't have a translation for the
        // Node's active langcode, create a new translation.
        if ($entity
          ->isTranslatable()) {
          if (!$entity
            ->hasTranslation($node
            ->language()
            ->getId())) {
            $entity = $entity
              ->addTranslation($node
              ->language()
              ->getId(), $entity
              ->toArray());
          }
          else {
            $entity = $entity
              ->getTranslation($node
              ->language()
              ->getId());
          }
        }
      }
      else {
        if ($node
          ->isNew()) {

          // Don't create a menu link if the node is being previewed.
          if ($form_state
            ->getTriggeringElement()['#id'] == 'edit-preview') {
            return;
          }

          // Create a new menu_link_content entity.
          $entity = MenuLinkContent::create([
            // Lets just reference the UUID for now, the link is not important for
            // token generation.
            'link' => [
              'uri' => 'internal:/node/' . $node
                ->uuid(),
            ],
            'langcode' => $node
              ->language()
              ->getId(),
          ]);
        }
        else {

          // Create a new menu_link_content entity.
          $entity = MenuLinkContent::create([
            'link' => [
              'uri' => 'entity:node/' . $node
                ->id(),
            ],
            'langcode' => $node
              ->language()
              ->getId(),
          ]);
        }
      }
      $entity->title->value = trim($values['title']);
      $entity->description->value = trim($values['description']);
      $entity->menu_name->value = $values['menu_name'];
      $entity->parent->value = $values['parent'];
      $entity->weight->value = isset($values['weight']) ? $values['weight'] : 0;
      $entity
        ->isDefaultRevision($node
        ->isDefaultRevision());
      $entity
        ->save();
      $node->menu_link = $entity;

      // Leave this for _menu_ui_node_save() to pick up so we don't end up with
      // duplicate menu-links.
      $form_state
        ->setValue([
        'menu',
        'entity_id',
      ], $entity
        ->id());
    }
  }
}