Verified Commit 7961aba4 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #2997790 by mohit_aghera, Keenegan, Grimreaper, lozsmile, catch,...

Issue #2997790 by mohit_aghera, Keenegan, Grimreaper, lozsmile, catch, angrytoast: Set MenuLinkContent getEntity to public visibility
parent 539ad62b
Loading
Loading
Loading
Loading
+2 −1
Changes for core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Menu\MenuLinkBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\menu_link_content\MenuLinkContentInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
@@ -130,7 +131,7 @@ public static function create(ContainerInterface $container, array $configuratio
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   *   If the entity ID and UUID are both invalid or missing.
   */
  protected function getEntity() {
  public function getEntity(): MenuLinkContentInterface {
    if (empty($this->entity)) {
      $entity = NULL;
      $storage = $this->entityTypeManager->getStorage('menu_link_content');
+26 −0
Changes for core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php: 26 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\entity_test\Entity\EntityTestExternal;
use Drupal\KernelTests\KernelTestBase;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\menu_link_content\Plugin\Menu\MenuLinkContent as MenuLinkContentPlugin;
use Drupal\system\Entity\Menu;
use Drupal\user\Entity\User;

@@ -433,4 +434,29 @@ public function testPendingRevisions() {
    $this->assertEquals('menu_parent', $violations[0]->getPropertyPath());
  }

  /**
   * Tests that getEntity() method returns correct value.
   */
  public function testMenuLinkContentGetEntity(): void {
    // Set up a custom menu link pointing to a specific path.
    $user = User::create(['name' => 'username']);
    $user->save();

    $title = $this->randomMachineName();
    $menu_link = MenuLinkContent::create([
      'title' => $title,
      'link' => [['uri' => 'internal:/' . $user->toUrl('collection')->getInternalPath()]],
      'menu_name' => 'menu_test',
    ]);
    $menu_link->save();
    $menu_tree = \Drupal::menuTree()->load('menu_test', new MenuTreeParameters());
    $this->assertCount(1, $menu_tree);
    /** @var \Drupal\Core\Menu\MenuLinkTreeElement $tree_element */
    $tree_element = reset($menu_tree);
    $this->assertInstanceOf(MenuLinkContentPlugin::class, $tree_element->link);
    $this->assertInstanceOf(MenuLinkContent::class, $tree_element->link->getEntity());
    $this->assertEquals($title, $tree_element->link->getEntity()->getTitle());
    $this->assertEquals($menu_link->id(), $tree_element->link->getEntity()->id());
  }

}