برنامه نویسی

افزونه های نوع بلوک در دروپال را گسترش دهید

یکی از کارهایی که من دوست دارم بیشترین کار را در دروپال انجام دهم ایجاد یک کلاس انتزاعی به عنوان “پایه” است که من در حال تعریف بلوک هایی هستم که در یک اسلاید استفاده می شود.

در این حالت ، به عنوان اسلاید بر اطلاعات صفحات طبقه بندی یک واژگان تأثیر می گذارد ، کاری که من انجام دادم ایجاد یک کلاس انتزاعی برای تعریف همه عناصر مشترک همه بلوک ها بود.



declare(strict_types=1);

namespace Drupal\project_term_area_study\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\taxonomy\TermInterface;

/**
 * Base class for all blocks used in area of study taxonomy pages.
 */
abstract class AreaOfStudyBaseBlock extends BlockBase implements ContainerFactoryPluginInterface {

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

  /**
   * Constructs a new MyCustomBlockBase object.
   *
   * @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\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    private readonly EntityTypeManagerInterface $entity_type_manager,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $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('entity_type.manager')
    );
  }

  /**
   */
  protected function getTermContext(): ?TermInterface {
    $term = $this->getContextValue('taxonomy_term');

    if ($term instanceof TermInterface) {
      return $term;
    }
    return NULL;
  }

}
حالت تمام صفحه را وارد کنید

از حالت تمام صفحه خارج شوید

آنچه هر دو بلوک که من از آن استفاده می کنم این است که هر دو به سرویس entity_type.manager نیاز دارند و از متن بلوک استفاده می کنند ، در این حالت برای بازیابی اصطلاحی که صفحه با آن مطابقت دارد.



declare(strict_types=1);

namespace Drupal\project_term_area_study\Plugin\Block;

use Drupal\Core\Url;

/**
 * Provides an area study degree standard name list block.
 *
 * @Block(
 *   id = "project_term_area_study_degree_standard_name_list",
 *   admin_label = @Translation("Area study degree standard name list"),
 *   category = @Translation("project"),
 *   context_definitions = {
 *     "taxonomy_term" = @ContextDefinition("entity:taxonomy_term")
 *   }
 * )
 */
final class AreaOfStudyDegreeStandardNameListBlock extends AreaOfStudyBaseBlock {

  /**
   * {@inheritdoc}
   */
  public function build(): array {
    $term = $this->getTermContext();
  }

}
حالت تمام صفحه را وارد کنید

از حالت تمام صفحه خارج شوید

در این حالت ، ما لازم نیست که نه سازنده یا روش ایجاد را برای بارگذاری خدمات تعریف شده در کلاس انتزاعی تعریف کنیم زیرا آنها یکسان هستند.

بلوک دوم به سرویس rendred نیاز دارد ، بنابراین اگر مجبور هستید سازنده و روش ایجاد را در آن سریع کنید. نکته مهم این است که والدین :: سازنده علاوه بر روشهای پیش فرض پایه شاخه های دروپال ، EntityTypeManager باید اتفاق بیفتد که این روشی است که کلاس انتزاعی را که تعریف کرده ایم بارگذاری می کند.



declare(strict_types=1);

namespace Drupal\project_term_area_study\Plugin\Block;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\taxonomy\TermInterface;
use Drupal\views\Views;
use Drupal\Core\Render\RendererInterface;

/**
 * Provides an area of study - others area of study block.
 *
 * @Block(
 *   id = "project_term_area_study_others_area_of_study",
 *   admin_label = @Translation("Area of study - others area of study"),
 *   category = @Translation("project"),
 *   context_definitions = {
 *     "taxonomy_term" = @ContextDefinition("entity:taxonomy_term")
 *   }
 * )
 */
final class AreaOfStudyOthersAreaOfStudyBlock extends AreaOfStudyBaseBlock {

  /**
   * The current renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs the plugin instance.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    EntityTypeManagerInterface $entityTypeManager,
    RendererInterface $renderer,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entityTypeManager);
    $this->renderer = $renderer;
  }

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

  /**
   * {@inheritdoc}
   */
  public function build(): array {
    $term = $this->getTermContext();

  }

  /**
   */
  protected function getListOfTerms(TermInterface $term) :?array {
// Code here.
  }

  /**
   */
  private function renderViewContent(string $view_id, string $display_id, $arguments): mixed {
// Code here.
  }

}

حالت تمام صفحه را وارد کنید

از حالت تمام صفحه خارج شوید

همچنین مهم است که اگرچه روش GetTermContext () ما آن را در کلاس انتزاعی در هر دو بلوک تعریف کرده ایم ، باید در “Block” تعریف شود تا دروپال بتواند زمینه را بارگیری کند.

نوشته های مشابه

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

دکمه بازگشت به بالا