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
Comment | File | Size | Author |
---|---|---|---|
#12 | interdiff-2150621-12.txt | 2.59 KB | damiankloip |
#12 | 2150621-12.patch | 13.86 KB | damiankloip |
Comments
Comment #1
damiankloip CreditAttribution: damiankloip commentedComment #2
damiankloip CreditAttribution: damiankloip commentedComment #3
dawehnerI do agree that separating this logic makes it way better to read and write.
Comment #4
Crell CreditAttribution: Crell commentedI'd be fine with this, but it needs committer approval as an API change.
Comment #5
msonnabaum CreditAttribution: msonnabaum commentedYeah, improvement.
Comment #6
catchLooks 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.
Comment #7
dawehnerI think we should also add this ifs to the breadcrumb builder as well.
Comment #8
damiankloip CreditAttribution: damiankloip commentedOops, 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 :)
Comment #9
dawehnerNote: this makes a potential breadcrumb contrib module really nice, as it will probably just take over all routes.
Comment #11
dawehnerThis really simplifies the needed logic. Thank you.
Comment #12
damiankloip CreditAttribution: damiankloip commentedSorry 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
Comment #13
Crell CreditAttribution: Crell commentedSilly Daniel, breaking his own patches...
Comment #14
tim.plunkettThe DX improvement here is major enough. We need to be sure the new replacements for the old menu stuff is as good as possible.
Comment #15
Dries CreditAttribution: Dries commentedI think this is a small improvement, but I don't think this is "Major". I'm happy to commit it though.
Comment #16
Dries CreditAttribution: Dries commentedCommitted to 8.x. Thanks.
Comment #17
TR CreditAttribution: TR commentedComment #18
Crell CreditAttribution: Crell commentedI've updated the existing change notice: https://drupal.org/node/2026025