routeMatch example

/** * Processes the batch item. * * @param array $data * Keyed array of data to process. * @param array $context * The batch context. */
  public static function processBatch($data, &$context) {
    if (!isset($context['results']['processed'])) {
      $context['results']['processed'] = 0;
      $context['results']['theme'] = \Drupal::service('theme.manager')->getActiveTheme(\Drupal::routeMatch())->getName();
    }
    $context['results']['processed']++;
  }

  /** * Finish batch. * * @param bool $success * Indicates whether the batch process was successful. * @param array $results * Results information passed from the processing callback. */
'update.module_update',
      'update.module_install',
      'update.status',
      'update.report_update',
      'update.report_install',
      'update.settings',
      'system.status',
      'update.manual_status',
      'update.confirmation_page',
      'system.themes_page',
    ];
    if (in_array(\Drupal::routeMatch()->getRouteName()$route_names)) {
      $this->keyValueStore->delete($key);
    }
    else {
      $projects = $this->keyValueStore->get($key[]);
    }
    return $projects;
  }

  /** * {@inheritdoc} */
  

    return $this->requestStack->getCurrentRequest();
  }

  /** * Gets the route match. * * @return \Drupal\Core\Routing\RouteMatchInterface */
  protected function getRouteMatch() {
    if (!$this->routeMatch) {
      $this->routeMatch = \Drupal::routeMatch();
    }
    return $this->routeMatch;
  }

  /** * Sets the request stack object to use. * * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack object. * * @return $this */

    }

    return $blocks;
  }

  /** * {@inheritdoc} */
  public function viewExposedFormBlocks() {
    // Avoid interfering with the admin forms.     $route_name = \Drupal::routeMatch()->getRouteName();
    if (str_starts_with($route_name, 'views_ui.')) {
      return;
    }
    $this->view->initHandlers();

    if ($this->usesExposed() && $this->getOption('exposed_block')) {
      /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form */
      $exposed_form = $this->getPlugin('exposed_form');
      return $exposed_form->renderExposedForm(TRUE);
    }
  }

  
/** * Initializes the active theme for a given route match. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The current route match. */
  protected function initTheme(RouteMatchInterface $route_match = NULL) {
    // Determine the active theme for the theme negotiator service. This includes     // the default theme as well as really specific ones like the ajax base theme.     if (!$route_match) {
      $route_match = \Drupal::routeMatch();
    }
    if ($route_match instanceof StackedRouteMatchInterface) {
      $route_match = $route_match->getMasterRouteMatch();
    }
    $theme = $this->themeNegotiator->determineActiveTheme($route_match);
    $this->activeTheme = $this->themeInitialization->initTheme($theme);
  }

  /** * {@inheritdoc} * * @todo Should we cache some of these information? */
protected function blockAccess(AccountInterface $account) {
    $access = $this->languageManager->isMultilingual() ? AccessResult::allowed() : AccessResult::forbidden();
    return $access->addCacheTags(['config:configurable_language_list']);
  }

  /** * {@inheritdoc} */
  public function build() {
    $build = [];
    $type = $this->getDerivativeId();
    $route_match = \Drupal::routeMatch();
    // If there is no route match, for example when creating blocks on 404 pages     // for logged-in users with big_pipe enabled using the front page instead.     $url = $route_match->getRouteObject() ? Url::fromRouteMatch($route_match) : Url::fromRoute('<front>');
    $links = $this->languageManager->getLanguageSwitchLinks($type$url);

    if (isset($links->links)) {
      $build = [
        '#theme' => 'links__language_block',
        '#links' => $links->links,
        '#attributes' => [
          'class' => [
            "

  public function attachments() {
    \Drupal::state()->set('common_test.hook_page_attachments.early_rendering', TRUE);
    $build = [
      '#title' => 'A title',
      'content' => ['#markup' => 'Some content'],
    ];
    return \Drupal::service('main_content_renderer.html')->renderResponse($build, \Drupal::requestStack()->getCurrentRequest(), \Drupal::routeMatch());
  }

}

function hook_preprocess_HOOK(&$variables) {
  // This example is from node_preprocess_html(). It adds the node type to   // the body classes, when on an individual node page or node preview page.   if (($node = \Drupal::routeMatch()->getParameter('node')) || ($node = \Drupal::routeMatch()->getParameter('node_preview'))) {
    if ($node instanceof NodeInterface) {
      $variables['node_type'] = $node->getType();
    }
  }
}

/** * Provides alternate named suggestions for a specific theme hook. * * This hook allows modules to provide alternative theme template name * suggestions. * * HOOK is the least-specific version of the hook being called. For example, if * '#theme' => 'node__article' is called, then hook_theme_suggestions_node() * will be invoked, not hook_theme_suggestions_node__article(). The specific * hook called (in this case 'node__article') is available in * $variables['theme_hook_original']. * * Implementations of this hook must be placed in *.module or *.theme files, or * must otherwise make sure that the hook implementation is available at * any given time. * * Suggestions must begin with the value of HOOK, followed by two underscores to be discoverable. * * In the following example, we provide suggestions to * node templates based bundle, id, and view mode. * * @code * function node_theme_suggestions_node(array $variables) { * $suggestions = []; * $node = $variables['elements']['#node']; * $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_'); * $suggestions[] = 'node__' . $sanitized_view_mode; * $suggestions[] = 'node__' . $node->bundle(); * $suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode; * $suggestions[] = 'node__' . $node->id(); * $suggestions[] = 'node__' . $node->id() . '__' . $sanitized_view_mode; * * return $suggestions; * } * * @endcode * * @param array $variables * An array of variables passed to the theme hook. Note that this hook is * invoked before any preprocessing. * * @return array * An array of theme suggestions. * * @see hook_theme_suggestions_HOOK_alter() */

function hook_entity_presave(\Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity instanceof ContentEntityInterface && $entity->isTranslatable()) {
    $route_match = \Drupal::routeMatch();
    \Drupal::service('content_translation.synchronizer')->synchronizeFields($entity$entity->language()->getId()$route_match->getParameter('source_langcode'));
  }
}

/** * Act on a specific type of entity before it is created or updated. * * You can get the original entity object from $entity->original when it is an * update of the entity. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity object. * * @ingroup entity_crud * @see hook_entity_presave() */
Home | Imprint | This part of the site doesn't use cookies.