Problem/Motivation

We don't have any field formatter

Proposed resolution

Add a menu_link field formatter (link or plain text) and a menu_link_breadcrumb field formatter that shows a breadcrumb trail with all the parents of the menu link.

Remaining tasks

Tests
Review

User interface changes

API changes

Data model changes

Comments

stefan.r created an issue. See original summary.

stefan.r’s picture

Status: Active » Needs review
StatusFileSize
new4.33 KB

We need to enable 8.x for issues :)

stefan.r’s picture

StatusFileSize
new9.71 KB
dawehner’s picture

I just added the 8.x-1.x release, so we can mark issues with it.

A general note: We add a couple of configuration bits, so we should also add the corresponding config dependencies.

  1. +++ b/src/Plugin/Field/FieldFormatter/MenuLinkBreadcrumbFormatter.php
    @@ -0,0 +1,161 @@
    +      $menu_links = [];
    +      $id = $items[$delta]->getMenuPluginId();
    +      $parent_ids = $this->menuLinkManager->getParentIds($id);
    +      if (!empty($parent_ids)) {
    +        foreach ($parent_ids as $parent_id) {
    +          if (!$this->getSetting('parents_only') || $parent_id != $id) {
    +            $menu_links[] = $this->menuLinkManager->createInstance($parent_id);
    +          }
    +        }
    +      }
    

    Could we use the MenuLinkTree here to save some lines of code?

  2. +++ b/src/Plugin/Field/FieldFormatter/MenuLinkBreadcrumbFormatter.php
    @@ -0,0 +1,161 @@
    +      $links = array_map(function ($link) {
    +        if ($this->getSetting('link_to_target')) {
    +          $url = $link->getUrlObject();
    +        }
    +        else {
    +          // Set URL to none if we don't want to link to the menu link target.
    +          $url = new Url('<none>');
    +        }
    +        return Link::fromTextAndUrl($link->getTitle(), $url);
    +      }, $menu_links);
    

    Nice!

  3. +++ b/src/Plugin/Field/FieldFormatter/MenuLinkBreadcrumbFormatter.php
    @@ -0,0 +1,161 @@
    +      // Set up the breadcrumb.
    +      $breadcrumb = new Breadcrumb();
    +      $breadcrumb->setLinks($links);
    +      $elements[$delta] = $breadcrumb->toRenderable();
    

    I love that these objects are reuseable.

stefan.r’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev
StatusFileSize
new6.43 KB
new15.19 KB

  • dawehner committed 0f384ac on 8.x-1.x authored by stefan.r
    Issue #2880938 by stefan.r: Add a normal field formatter and a...
dawehner’s picture

Status: Needs review » Fixed

Thank you for the contribution. Committed and pushed to 8.x-1.x

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.