diff --git a/opencase.services.yml b/opencase.services.yml new file mode 100644 index 0000000..eee55d8 --- /dev/null +++ b/opencase.services.yml @@ -0,0 +1,8 @@ +services: + opencase.breadcrumb: + # The namespace + classname from your BreadcrumbBuilderInterface class + class: Drupal\opencase\Breadcrumb\BreadcrumbBuilder + + # Priority determines the order in which Breadcrumb services run. + tags: + - { name: breadcrumb_builder, priority: 100 } \ No newline at end of file diff --git a/src/Breadcrumb/BreadcrumbBuilder.php b/src/Breadcrumb/BreadcrumbBuilder.php new file mode 100644 index 0000000..16594b2 --- /dev/null +++ b/src/Breadcrumb/BreadcrumbBuilder.php @@ -0,0 +1,97 @@ +title ?? $this->getPageTitle($route_match); + $this->breadcrumb->addLink(Link::createFromRoute($title, '')); + } + + /** + * {@inheritdoc} + */ + public function applies(RouteMatchInterface $route_match) { + return TRUE; + } + + private function getPageTitle($route_match) { + $request = \Drupal::request(); + return \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject()); + } + + private function addLinksForOrganisation(ContentEntityBase $entity) { + $fields = ['field_umbrella_client']; + $this->addLinks($entity, $fields); + } + private function addLinksForCase(OCCase $case) { + $fields = ['client', 'field_project']; + $this->addLinksForOrganisation($case->get('client')->entity); + $this->addLinks($case, $fields); + } + + private function addLinks(ContentEntityBase $entity, array $fields) { + foreach ($fields as $field) { + if ($entity->hasField($field) && !$entity->get($field)->isEmpty()) { + $this->breadcrumb->addLink($entity->$field->entity->toLink()); + } + } + } + + /** + * {@inheritdoc} + */ + public function build(RouteMatchInterface $route_match) { + $this->breadcrumb = new Breadcrumb(); + $this->breadcrumb->addLink(Link::createFromRoute('Home', '')); + $params = $route_match->getParameters(); + + if ($params->has('oc_activity')) { + $activity = $params->get('oc_activity'); + if (!$activity->get('oc_case')->isEmpty()){ + $this->addLinksForCase($activity->get('oc_case')->entity); + } + $this->addLinks($activity, ['client', 'oc_case']); + $this->title = $activity->getName() ?? $activity->type->entity->label(); + } + + elseif ($params->has('oc_organisation')) { + $this->addLinksForOrganisation($params->get('oc_organisation')); + } + + elseif ($params->has('oc_case')) { + $this->addLinksForCase($params->get('oc_case')); + } + + elseif ($params->has('oc_case_fee')) { + $case_provision = $params->get('oc_case_fee'); + $this->addLinksForCase($case_provision->get('oc_case')->entity); + $this->addLinks($case_provision, ['oc_case']); + $this->title = "Fee"; + } + + elseif ($params->has('oc_case_provision')) { + $case_provision = $params->get('oc_case_provision'); + $this->addLinksForCase($case_provision->get('oc_case')->entity); + $this->addLinks($case_provision, ['oc_case']); + $this->title = $case_provision->type->entity->label(); + } + + $this->addTitle($route_match); + + // Don't forget to add cache control by a route. + // Otherwise all pages will have the same breadcrumb. + $this->breadcrumb->addCacheContexts(['route']); + return $this->breadcrumb; + } + +} \ No newline at end of file