You are here

public function YamlFormBreadcrumbBuilder::build in YAML Form 8

Builds the breadcrumb.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The current route match.

Return value

\Drupal\Core\Breadcrumb\Breadcrumb A breadcrumb.

Overrides BreadcrumbBuilderInterface::build

File

src/BreadCrumb/YamlFormBreadcrumbBuilder.php, line 78

Class

YamlFormBreadcrumbBuilder
Provides a form breadcrumb builder.

Namespace

Drupal\yamlform\BreadCrumb

Code

public function build(RouteMatchInterface $route_match) {
  $route_name = $route_match
    ->getRouteName();
  if ($this->type == 'yamlform_source_entity') {

    /** @var \Drupal\yamlform\YamlFormRequestInterface $request_handler */
    $request_handler = \Drupal::service('yamlform.request');
    $source_entity = $request_handler
      ->getCurrentSourceEntity([
      'yamlform',
      'yamlform_submission',
    ]);
    $entity_type = $source_entity
      ->getEntityTypeId();
    $entity_id = $source_entity
      ->id();
    $breadcrumb = new Breadcrumb();
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Home'), '<front>'));
    $breadcrumb
      ->addLink($source_entity
      ->toLink());
    if ($yamlform_submission = $route_match
      ->getParameter('yamlform_submission')) {
      if (strpos($route_match
        ->getRouteName(), 'yamlform.user.submission') !== FALSE) {
        $breadcrumb
          ->addLink(Link::createFromRoute($this
          ->t('Submissions'), "entity.{$entity_type}.yamlform.user.submissions", [
          $entity_type => $entity_id,
        ]));
      }
      elseif ($source_entity
        ->access('yamlform_submission_view') || $yamlform_submission
        ->access('view_any')) {
        $breadcrumb
          ->addLink(Link::createFromRoute($this
          ->t('Results'), "entity.{$entity_type}.yamlform.results_submissions", [
          $entity_type => $entity_id,
        ]));
      }
      elseif ($yamlform_submission
        ->access('view_own')) {
        $breadcrumb
          ->addLink(Link::createFromRoute($this
          ->t('Results'), "entity.{$entity_type}.yamlform.user.submissions", [
          $entity_type => $entity_id,
        ]));
      }
    }
  }
  else {
    $breadcrumb = new Breadcrumb();
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Home'), '<front>'));
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Administration'), 'system.admin'));
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Structure'), 'system.admin_structure'));
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Forms'), 'entity.yamlform.collection'));
    switch ($this->type) {
      case 'yamlform_template':
        $breadcrumb
          ->addLink(Link::createFromRoute('Templates', 'entity.yamlform.templates'));
        break;
      case 'yamlform_element':

        /** @var \Drupal\yamlform\YamlFormInterface $yamlform */
        $yamlform = $route_match
          ->getParameter('yamlform');
        $breadcrumb
          ->addLink(Link::createFromRoute($yamlform
          ->label(), 'entity.yamlform.canonical', [
          'yamlform' => $yamlform
            ->id(),
        ]));
        $breadcrumb
          ->addLink(Link::createFromRoute('Elements', 'entity.yamlform.edit_form', [
          'yamlform' => $yamlform
            ->id(),
        ]));
        break;
      case 'yamlform_handler':
        if ($route_name != 'yamlform.handler_plugins') {

          /** @var \Drupal\yamlform\YamlFormInterface $yamlform */
          $yamlform = $route_match
            ->getParameter('yamlform');
          $breadcrumb
            ->addLink(Link::createFromRoute($yamlform
            ->label(), 'entity.yamlform.canonical', [
            'yamlform' => $yamlform
              ->id(),
          ]));
          $breadcrumb
            ->addLink(Link::createFromRoute('Emails / Handlers', 'entity.yamlform.handlers_form', [
            'yamlform' => $yamlform
              ->id(),
          ]));
        }
        break;
      case 'yamlform_options':
        if ($route_name != 'entity.yamlform_options.collection') {
          $breadcrumb
            ->addLink(Link::createFromRoute($this
            ->t('Options'), 'entity.yamlform_options.collection'));
        }
        break;
      case 'yamlform_submission':

        /** @var \Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission */
        $yamlform_submission = $route_match
          ->getParameter('yamlform_submission');
        $yamlform = $yamlform_submission
          ->getYamlForm();
        $breadcrumb
          ->addLink(Link::createFromRoute($yamlform
          ->label(), 'entity.yamlform.canonical', [
          'yamlform' => $yamlform
            ->id(),
        ]));
        $breadcrumb
          ->addLink(Link::createFromRoute($this
          ->t('Results'), 'entity.yamlform.results_submissions', [
          'yamlform' => $yamlform
            ->id(),
        ]));
        break;
      case 'yamlform_user_submissions':

        /** @var \Drupal\yamlform\YamlFormInterface $yamlform */
        $yamlform = $route_match
          ->getParameter('yamlform');
        $breadcrumb = new Breadcrumb();
        $breadcrumb
          ->addLink(Link::createFromRoute($yamlform
          ->label(), 'entity.yamlform.canonical', [
          'yamlform' => $yamlform
            ->id(),
        ]));
        break;
      case 'yamlform_user_submission':

        /** @var \Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission */
        $yamlform_submission = $route_match
          ->getParameter('yamlform_submission');
        $yamlform = $yamlform_submission
          ->getYamlForm();
        $breadcrumb = new Breadcrumb();
        $breadcrumb
          ->addLink(Link::createFromRoute($yamlform
          ->label(), 'entity.yamlform.canonical', [
          'yamlform' => $yamlform
            ->id(),
        ]));
        $breadcrumb
          ->addLink(Link::createFromRoute($this
          ->t('Submissions'), 'entity.yamlform.user.submissions', [
          'yamlform' => $yamlform
            ->id(),
        ]));
        break;
    }
  }

  // This breadcrumb builder is based on a route parameter, and hence it
  // depends on the 'route' cache context.
  $breadcrumb
    ->addCacheContexts([
    'route',
  ]);
  return $breadcrumb;
}