drupalorg-1.0.x-dev/src/Plugin/Block/DrupalOrgDocumentationTree.php

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

namespace Drupal\drupalorg\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;

/**
 * Provides a 'DrupalOrg Documentation Tree' Block.
 *
 * @Block(
 *   id = "drupalorg_documentation_tree",
 *   admin_label = @Translation("DrupalOrg Documentation Tree"),
 * )
 */
class DrupalOrgDocumentationTree extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected RouteMatchInterface $routeMatch;

  /**
   * Entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected EntityTypeManagerInterface $entityTypeManager;

  /**
   * Constructs a new DrupalOrgSponsorWidget instance.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->routeMatch = $route_match;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@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')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    /** @var \Drupal\node\NodeInterface $node */
    $node = $this->routeMatch->getParameter('node');
    if (!in_array($node->bundle(), ['documentation', 'guide'])) {
      return [];
    }

    $children = $this->getChildren($node);
    if (empty($children)) {
      return [];
    }

    $cache_tags = [
      'node:' . $node->id(),
    ];
    $markup = '';
    foreach ($children as $child) {
      /** @var \Drupal\node\NodeInterface $child */
      $cache_tags[] = 'node:' . $child->id();
      $title = $child->toLink($child->getTitle())->toString();
      $summary = '';
      $children_markup = '';
      if ($child->hasField('field_summary') && !$child->field_summary->isEmpty()) {
        $summary = '<p>' . $child->get('field_summary')->getValue()[0]['value'] . '</p>';
      }
      if ($child->getType() === 'guide') {
        $second_level_children = $this->getChildren($child);
        if (!empty($second_level_children)) {
          $children_markup_links = [];
          foreach ($second_level_children as $second_level_child) {
            $cache_tags[] = 'node:' . $second_level_child->id();
            $children_markup_links[] = '<li>' . $second_level_child->toLink($second_level_child->getTitle())->toString() . '</li>';
          }
          $children_markup = '<ul class="guide-contents">' . implode('', $children_markup_links) . '</ul>';
        }
      }
      $markup .= '<section><h2>' . $title . '</h2>' . $summary . $children_markup . '</section>';
    }

    return [
      '#prefix' => '<div class="documentation-tree">',
      '#suffix' => '</div>',
      '#markup' => $markup,
      '#cache' => [
        'contexts' => ['url'],
        'tags' => $cache_tags,
      ],
    ];
  }

  /**
   * Get the children nodes of a guide.
   *
   * @param \Drupal\node\NodeInterface $node
   *   Guide node.
   *
   * @return \Drupal\node\NodeInterface[]
   *   Array of children nodes, if any.
   */
  protected function getChildren(NodeInterface $node): array {
    /** @var \Drupal\Node\NodeStorage $nodeStorage */
    $nodeStorage = $this->entityTypeManager->getStorage('node');
    $query = $nodeStorage->getQuery()->accessCheck(FALSE);
    $query->condition('status', NodeInterface::PUBLISHED)
      ->condition('type', ['documentation', 'guide'], 'IN')
      ->condition('og_group_ref_documentation.entity:node.nid', $node->id())
      ->sort('field_weight')
      ->sort('title');

    return $nodeStorage->loadMultiple($query->execute());
  }

}

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

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