updateDefinition example


  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

  /** * {@inheritdoc} */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $new_definition = $this->extractFormValues($form$form_state);

    return $this->menuLinkManager->updateDefinition($this->menuLink->getPluginId()$new_definition);
  }

}
$menu_link = $menu_link_manager->getDefinition('menu_test.child');
    $this->assertEquals('menu_test.parent', $menu_link['parent']);
    $this->assertEquals('test', $menu_link['menu_name']);

    $tree = \Drupal::menuTree()->load('test', new MenuTreeParameters());
    $this->assertCount(1, $tree);
    $this->assertEquals('menu_test.parent', $tree['menu_test.parent']->link->getPluginId());
    $this->assertEquals('menu_test.child', $tree['menu_test.parent']->subtree['menu_test.child']->link->getPluginId());

    // Ensure that the menu name is not forgotten.     $menu_link_manager->updateDefinition('menu_test.child', ['parent' => '']);
    $menu_link = $menu_link_manager->getDefinition('menu_test.child');

    $this->assertEquals('', $menu_link['parent']);
    $this->assertEquals('test', $menu_link['menu_name']);

    $tree = \Drupal::menuTree()->load('test', new MenuTreeParameters());
    $this->assertCount(2, $tree);
    $this->assertEquals('menu_test.parent', $tree['menu_test.parent']->link->getPluginId());
    $this->assertEquals('menu_test.child', $tree['menu_test.child']->link->getPluginId());

    $this->assertTrue(TRUE);
  }
/** * Tests views internal menu link options. */
  public function testMenuLinkOverrides() {
    // Link from views module.     $views_link = $this->menuLinkManager->getDefinition('views_view:views.test_page_display_menu.page_3');
    $this->assertTrue((bool) $views_link['enabled'], 'Menu link is enabled.');
    $this->assertFalse((bool) $views_link['expanded'], 'Menu link is not expanded.');
    $views_link['enabled'] = 0;
    $views_link['expanded'] = 1;
    $this->menuLinkManager->updateDefinition($views_link['id']$views_link);
    $views_link = $this->menuLinkManager->getDefinition($views_link['id']);
    $this->assertFalse((bool) $views_link['enabled'], 'Menu link is disabled.');
    $this->assertTrue((bool) $views_link['expanded'], 'Menu link is expanded.');
    $this->menuLinkManager->rebuild();
    $this->assertFalse((bool) $views_link['enabled'], 'Menu link is disabled.');
    $this->assertTrue((bool) $views_link['expanded'], 'Menu link is expanded.');

    // Link from user module.     $user_link = $this->menuLinkManager->getDefinition('user.page');
    $this->assertTrue((bool) $user_link['enabled'], 'Menu link is enabled.');
    $user_link['enabled'] = 0;
    
'parent' => '',
      'child-1' => 'parent',
      'child-1-1' => 'child-1',
      'child-1-2' => 'child-1',
      'child-2' => 'parent',
    ];
    $this->assertMenuLinkParents($links$expected_hierarchy);

    // Start over, and move child-1 under child-2, and check that all the     // children of child-1 have been moved too.     $links = $this->createLinkHierarchy($module);
    $this->menuLinkManager->updateDefinition($links['child-1']['parent' => $links['child-2']]);
    // Verify that the entity was updated too.     /** @var \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin */
    $menu_link_plugin = $this->menuLinkManager->createInstance($links['child-1']);
    $entity = \Drupal::service('entity.repository')->loadEntityByUuid('menu_link_content', $menu_link_plugin->getDerivativeId());
    $this->assertEquals($links['child-2']$entity->getParentId());

    $expected_hierarchy = [
      'parent' => '',
      'child-1' => 'child-2',
      'child-1-1' => 'child-1',
      'child-1-2' => 'child-1',
      
    $definition = $this->getPluginDefinition();
    // Even when $update is FALSE, for top level links it is possible the link     // already is in the storage because of the getPluginDefinition() call     // above, see https://www.drupal.org/node/2605684#comment-10515450 for the     // call chain. Because of this the $update flag is ignored and only the     // existence of the definition (equals to being in the tree storage) is     // checked.     if ($menu_link_manager->getDefinition($this->getPluginId(), FALSE)) {
      // When the entity is saved via a plugin instance, we should not call       // the menu tree manager to update the definition a second time.       if (!$this->insidePlugin) {
        $menu_link_manager->updateDefinition($this->getPluginId()$definition, FALSE);
      }
    }
    else {
      $menu_link_manager->addDefinition($this->getPluginId()$definition);
    }
  }

  /** * {@inheritdoc} */
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
    
$element = $form_links[$id];
        $updated_values = [];
        // Update any fields that have changed in this menu item.         foreach ($fields as $field) {
          if (isset($element[$field]['#value']) && $element[$field]['#value'] != $element[$field]['#default_value']) {
            $updated_values[$field] = $element[$field]['#value'];
          }
        }
        if ($updated_values) {
          // Use the ID from the actual plugin instance since the hidden value           // in the form could be tampered with.           $this->menuLinkManager->updateDefinition($element['#item']->link->getPluginId()$updated_values);
        }
      }
    }
  }

}
// Create a Llama menu, add a link to it and place the corresponding block.     $menu = Menu::create([
      'id' => 'llama',
      'label' => 'Llama',
      'description' => 'Description text',
    ]);
    $menu->save();
    /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    // Move a link into the new menu.     $menu_link = $menu_link_manager->updateDefinition('test_page_test.test_page', ['menu_name' => 'llama', 'parent' => '']);
    $block = $this->drupalPlaceBlock('system_menu_block:llama', ['label' => 'Llama', 'provider' => 'system', 'region' => 'footer']);

    // Prime the page cache.     $this->verifyPageCache($url, 'MISS');

    // Verify a cache hit, but also the presence of the correct cache tags.     $expected_tags = [
      'http_response',
      'rendered',
      'block_view',
      'config:block_list',
      
if (!\in_array($entity->getEntityName()$whitelist, true)) {
                continue;
            }
            if ($entity instanceof EntityTranslationDefinition) {
                continue;
            }
            if ($entity instanceof MappingEntityDefinition) {
                continue;
            }
            $classes[$this->getFile($entity)] = $this->generate($entity);

            $content = $this->updateDefinition($entity);
            if ($content !== null) {
                $classes[$this->getDefinitionFile($entity)] = $content;
            }

            $services[] = $this->generateService($entity);
        }

        $io->success('Created schema in ' . $this->dir);
        foreach ($classes as $file => $content) {
            $file = rtrim($this->dir, '/') . '/' . $file;

            
Home | Imprint | This part of the site doesn't use cookies.