Updated: Comment #N

Problem/Motivation

Currently the BreadcrumbBuilderInterface consists of just a build() method. This means the logic that decides whether the breadcrumb builder applies on a certain page and the actual building of the breadcrumb live in build(). So we rely on that returning NULL to determine whether or not it applies. The build() method should be for...building.

Proposed resolution

Add an applies() method to BreadcrumbBuilderInterface so the applies logic can live there instead. Then we have a nice separation between the two, and therefore (I think) a better API. This is more in line with access checkers etc..

So, for example, CommnetBreacrumbBuilder looks like this:

  /**
   * {@inheritdoc}
   */
  public function build(array $attributes) {
    if (isset($attributes[RouteObjectInterface::ROUTE_NAME]) && $attributes[RouteObjectInterface::ROUTE_NAME] == 'comment.reply'
      && isset($attributes['entity_type'])
      && isset($attributes['entity_id'])
      && isset($attributes['field_name'])
      ) {
      $breadcrumb[] = $this->l($this->t('Home'), '<front>');
      $entity = $this->entityManager
        ->getStorageController($attributes['entity_type'])
        ->load($attributes['entity_id']);
      $uri = $entity->uri();
      $breadcrumb[] = l($entity->label(), $uri['path'], $uri['options']);
      return $breadcrumb;
    }
  }

when it could look like:

  /**
   * {@inheritdoc}
   */
  public function applies(array $attributes) {
    return isset($attributes[RouteObjectInterface::ROUTE_NAME]) && $attributes[RouteObjectInterface::ROUTE_NAME] == 'comment.reply'
    && isset($attributes['entity_type'])
    && isset($attributes['entity_id'])
    && isset($attributes['field_name']);
  }

  /**
   * {@inheritdoc}
   */
  public function build(array $attributes) {
    $breadcrumb[] = $this->l($this->t('Home'), '<front>');
    $entity = $this->entityManager
      ->getStorageController($attributes['entity_type'])
      ->load($attributes['entity_id']);
    $uri = $entity->uri();
    $breadcrumb[] = l($entity->label(), $uri['path'], $uri['options']);
    return $breadcrumb;
  }

IMO, that is much much better.

Remaining tasks

Conversion patch

User interface changes

None.

API changes

Addition of applies() method to BreadcrumbBuilderInterface

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

damiankloip’s picture

Issue summary: View changes
damiankloip’s picture

Status: Active » Needs review
dawehner’s picture

I do agree that separating this logic makes it way better to read and write.

Crell’s picture

I'd be fine with this, but it needs committer approval as an API change.

msonnabaum’s picture

Yeah, improvement.

catch’s picture

Looks like a good improvement. While it's an API change, it's better developer experience and I think it's unlikely that there's any breadcrumb builders in contrib yet. Would like a second opinion from another committer though.

dawehner’s picture

+++ b/core/modules/forum/lib/Drupal/forum/ForumBreadcrumbBuilder.php
@@ -58,18 +58,25 @@ public function __construct(EntityManagerInterface $entity_manager, ConfigFactor
+    if ($route_name == 'node.view' && isset($attributes['node'])) {
+      if ($this->forumManager->checkNodeType($attributes['node'])) {
...
+    if ($route_name == 'forum.page' && isset($attributes['taxonomy_term'])) {
+      return $this->forumTermBreadcrumb($attributes['taxonomy_term']);

I think we should also add this ifs to the breadcrumb builder as well.

damiankloip’s picture

FileSize
11.27 KB
1.62 KB

Oops, forgot to do a 'proper job' on the forum builder.

EDIT: Sorry dawehner, looks like I x posted this patch with your comment in #7 - looks like it fixed exactly that though :)

dawehner’s picture

Note: this makes a potential breadcrumb contrib module really nice, as it will probably just take over all routes.

The last submitted patch, d8.breadcrumb-builder-applies.patch, failed testing.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

This really simplifies the needed logic. Thank you.

damiankloip’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
13.86 KB
2.59 KB

Sorry Daniel, can you re-RTBC please? I just noticed that a new BreadcrumbManagerTest got committed a few days ago. So this patch will fail now. It was your patch too :p

Crell’s picture

Status: Needs review » Reviewed & tested by the community

Silly Daniel, breaking his own patches...

tim.plunkett’s picture

Priority: Normal » Major
Issue tags: +MenuSystemRevamp

The DX improvement here is major enough. We need to be sure the new replacements for the old menu stuff is as good as possible.

Dries’s picture

I think this is a small improvement, but I don't think this is "Major". I'm happy to commit it though.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 8.x. Thanks.

TR’s picture

Issue tags: +Needs change record
Crell’s picture

Issue tags: -Needs change record

I've updated the existing change notice: https://drupal.org/node/2026025

Status: Fixed » Closed (fixed)

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