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

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

namespace Drupal\drupalorg\Plugin\Block;

use Drupal\Core\Url;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a 'DrupalOrg Documentation Issue Submission' Block.
 *
 * @Block(
 *   id = "drupalorg_documentation_issue_submission",
 *   admin_label = @Translation("DrupalOrg Documentation Issue Submission")
 * )
 */
class DrupalOrgDocumentationIssue extends BlockBase implements ContainerFactoryPluginInterface {

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

  /**
   * Constructs a new DrupalOrgDocumentationIssue 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.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->routeMatch = $route_match;
  }

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

  /**
   * {@inheritdoc}
   */
  public function build() {
    $node = $this->routeMatch->getParameter('node');

    if ($node && $node->bundle() == 'documentation') {
      $node_title = $node->getTitle();

      $formatted_title = sprintf(
        'Suggestion for: %s',
        $node_title
      );

      // Get the absolute URL of the current node.
      $node_url = $node->toUrl()->setAbsolute()->toString();

      $url = Url::fromUri('https://www.drupal.org/node/add/project-issue/documentation', [
        'query' => [
          'title' => $formatted_title,
          'body' => 'The documentation can be found at: ' . $node_url,
        ],
      ]);

      $cache_tags = [
        'node:' . $node->id(),
      ];

      return [
        '#theme' => 'drupalorg_documentation_issue_submission',
        '#url' => $url->toString(),
        '#cache' => [
          'contexts' => ['url'],
          'tags' => $cache_tags,
        ],
      ];
    }

    return [];
  }

}

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

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