You are here

public function WebformBreadcrumbBuilder::applies in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Breadcrumb/WebformBreadcrumbBuilder.php \Drupal\webform\Breadcrumb\WebformBreadcrumbBuilder::applies()

Whether this breadcrumb builder should be used to build the breadcrumb.

Parameters

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

Return value

bool TRUE if this builder should be used or FALSE to let other builders decide.

Overrides BreadcrumbBuilderInterface::applies

File

src/Breadcrumb/WebformBreadcrumbBuilder.php, line 75

Class

WebformBreadcrumbBuilder
Provides a webform breadcrumb builder.

Namespace

Drupal\webform\Breadcrumb

Code

public function applies(RouteMatchInterface $route_match) {
  $route_name = $route_match
    ->getRouteName();

  // All routes must begin or contain 'webform.
  if (strpos($route_name, 'webform') === FALSE) {
    return FALSE;
  }
  $args = explode('.', $route_name);

  // Skip all config_translation routes except the overview
  // and allow Drupal to use the path as the breadcrumb.
  if (strpos($route_name, 'config_translation') !== FALSE && !in_array($route_name, [
    'entity.webform.config_translation_overview',
    'config_translation.item.overview.webform.config',
    'config_translation.item.add.webform.config',
    'config_translation.item.edit.webform.config',
    'config_translation.item.delete.webform.config',
  ])) {
    return FALSE;
  }
  try {
    $path = Url::fromRouteMatch($route_match)
      ->toString();
  } catch (\Exception $exception) {
    $path = '';
  }

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = $route_match
    ->getParameter('webform') instanceof WebformInterface ? $route_match
    ->getParameter('webform') : NULL;

  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $route_match
    ->getParameter('webform_submission') instanceof WebformSubmissionInterface ? $route_match
    ->getParameter('webform_submission') : NULL;
  if (count($args) > 2 && $args[0] === 'entity' && ($args[2] === 'webform' || $args[2] === 'webform_submission')) {
    $this->type = 'webform_source_entity';
  }
  elseif ($route_name === 'webform.reports_plugins.elements.test') {
    $this->type = 'webform_plugins_elements';
  }
  elseif (strpos($route_name, 'webform.help.') === 0) {
    $this->type = 'webform_help';
  }
  elseif (strpos($route_name, 'entity.webform_ui.element') === 0) {
    $this->type = 'webform_element';
  }
  elseif (strpos($route_name, 'entity.webform.handler.') === 0) {
    $this->type = 'webform_handler';
  }
  elseif (strpos($route_name, 'entity.webform.variant.') === 0) {
    $this->type = 'webform_variant';
  }
  elseif ($webform_submission && strpos($route_name, '.webform.user.submission') !== FALSE) {
    $this->type = 'webform_user_submission';
  }
  elseif (strpos($route_name, '.webform.user.submissions') !== FALSE) {
    $this->type = 'webform_user_submissions';
  }
  elseif (strpos($route_name, '.webform.user.drafts') !== FALSE) {
    $this->type = 'webform_user_drafts';
  }
  elseif ($webform_submission && $webform_submission
    ->access('admin')) {
    $this->type = 'webform_submission';
  }
  elseif ($webform && $webform
    ->access('admin')) {
    $this->type = $webform
      ->isTemplate() && $this->moduleHandler
      ->moduleExists('webform_templates') ? 'webform_template' : 'webform';
  }
  elseif (strpos($path, 'admin/structure/webform/test/') !== FALSE) {
    $this->type = 'webform_test';
  }
  elseif (strpos($path, 'admin/structure/webform/options/') !== FALSE) {
    $this->type = 'webform_options';
  }
  elseif (strpos($path, 'admin/structure/webform/config/') !== FALSE) {
    $this->type = 'webform_config';
  }
  else {
    $this->type = NULL;
  }
  return $this->type ? TRUE : FALSE;
}