localgov_step_by_step-2.1.10/src/Plugin/Block/StepPartOfBlock.php

src/Plugin/Block/StepPartOfBlock.php
<?php

namespace Drupal\localgov_step_by_step\Plugin\Block;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a 'StepPartOfBlock' block.
 *
 * @Block(
 *  id = "step_part_of_block",
 *  admin_label = @Translation("Part of step heading"),
 * )
 */
class StepPartOfBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * Drupal\Core\Routing\CurrentRouteMatch definition.
   *
   * @var \Drupal\Core\Routing\CurrentRouteMatch
   */
  protected $currentRouteMatch;

  /**
   * Drupal\Core\Entity\EntityTypeManagerInterface definition.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Guide node being displayed.
   *
   * @var \Drupal\node\NodeInterface
   */
  protected $node;

  /**
   * Current route object.
   *
   * @var \Drupal\Core\Routing\ResettableStackedRouteMatchInterface
   */
  protected $routeMatch;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('current_route_match'),
      $container->get('entity_type.manager')
    );
  }

  /**
   * Initialise new content block instance.
   *
   * @param array $configuration
   *   The plugin configuration.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Routing\CurrentRouteMatch $route_match
   *   The route match service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, CurrentRouteMatch $route_match, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->routeMatch = $route_match;
    $this->entityTypeManager = $entity_type_manager;
    if ($this->routeMatch->getParameter('node')) {
      $this->node = $this->routeMatch->getParameter('node');
      if (!$this->node instanceof NodeInterface) {
        $node_storage = $this->entityTypeManager->getStorage('node');
        $this->node = $node_storage->load($this->node);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build = [];

    if ($this->node->localgov_step_parent && $this->node->localgov_step_parent->entity) {
      $langcode = $this->node->langcode->value;
      // If this is the default translation.
      if ($this->node->isDefaultTranslation()) {
        $parent_entity = $this->node->localgov_step_parent->entity;
      }
      // If this is a translation and the parent's also translated.
      elseif ($this->node->localgov_step_parent->entity->hasTranslation($langcode)) {
        $parent_entity = $this->node->localgov_step_parent->entity->getTranslation($langcode);
      }
      else {
        // If the current node is translated, but the parent node isn't.
        $parent_entity = $this->node->localgov_step_parent->entity;
      }
      $build[] = [
        '#theme' => 'step_by_step_part_of_block',
        '#label' => $parent_entity->label(),
        '#url' => $parent_entity->toUrl(),
      ];
      $build['#cache']['contexts'][] = 'languages:language_interface';
    }

    return $build;
  }

  /**
   * {@inheritdoc}
   */
  protected function blockAccess(AccountInterface $account) {
    if ($this->node && $this->node->bundle() == 'localgov_step_by_step_page') {
      return AccessResult::allowed();
    }
    return AccessResult::neutral();
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return Cache::mergeContexts(parent::getCacheContexts(), ['languages:language_interface', 'route']);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc