Skip to content
Snippets Groups Projects
Commit 7fc778e0 authored by Stephen Mustgrave's avatar Stephen Mustgrave
Browse files

Resolve #3497967 "Setup gitlab"

parent 5ef29b63
No related branches found
No related tags found
1 merge request!11Resolve #3497967 "Setup gitlab"
Pipeline #388926 passed
################# DrupalCI GitLabCI template
#
# Gitlab-ci.yml to replicate DrupalCI testing for Contrib
#
# With thanks to:
# * The GitLab Acceleration Initiative participants
# * DrupalSpoons
################
################
# Guidelines
#
# This template is designed to give any Contrib maintainer everything they need to test, without requiring modification. It is also designed to keep up to date with Core Development automatically through the use of include files that can be centrally maintained.
#
# However, you can modify this template if you have additional needs for your project.
################
################
# Includes
#
# Additional configuration can be provided through includes.
# One advantage of include files is that if they are updated upstream, the changes affect all pipelines using that include.
#
# Includes can be overridden by re-declaring anything provided in an include, here in gitlab-ci.yml
# https://docs.gitlab.com/ee/ci/yaml/includes.html#override-included-configuration-values
################
include:
################
# DrupalCI includes:
# As long as you include this, any future includes added by the Drupal Association will be accessible to your pipelines automatically.
# View these include files at https://git.drupalcode.org/project/gitlab_templates/
################
- project: $_GITLAB_TEMPLATES_REPO
ref: $_GITLAB_TEMPLATES_REF
file:
- '/includes/include.drupalci.main.yml'
- '/includes/include.drupalci.variables.yml'
- '/includes/include.drupalci.workflows.yml'
################
# Pipeline configuration variables
#
# These are the variables provided to the Run Pipeline form that a user may want to override.
#
# Docs at https://git.drupalcode.org/project/gitlab_templates/-/blob/main/includes/include.drupalci.variables.yml
################
variables:
_PHPUNIT_CONCURRENT: '1'
OPT_IN_TEST_MAX_PHP: '1'
OPT_IN_TEST_PREVIOUS_MAJOR: '1'
OPT_IN_TEST_NEXT_MAJOR: '1'
OPT_IN_TEST_NEXT_MINOR: '1'
# CSpell Customization.
# _CSPELL_WORDS: "Booktree,Centry,orif,multipage,plid,Mustgrave,smustgrave,Wolanin,pwolanin"
......@@ -2,27 +2,34 @@
namespace Drupal\entity_block\Plugin\Block;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity_block\Plugin\Derivative\EntityBlockDeriver;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the block for similar articles.
*
* @Block(
* id = "entity_block",
* admin_label = @Translation("Entity block"),
* deriver = "Drupal\entity_block\Plugin\Derivative\EntityBlock"
* )
*/
#[Block(
id: "entity_block",
admin_label: new TranslatableMarkup("Entity block"),
deriver: EntityBlockDeriver::class
)]
class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
......@@ -37,21 +44,14 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
*
* @var string
*/
protected $entityTypeName;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
public $entityTypeManager;
protected string $entityTypeName;
/**
* The entity storage for our entity type.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $entityStorage;
protected EntityStorageInterface $entityStorage;
/**
* The view builder for our entity type.
......@@ -65,52 +65,47 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
*
* @var array
*/
protected $view_mode_options;
protected array $viewModeOptions;
/**
* An array of counters for the recursive rendering protection.
*
* @var array
*/
protected static $recursiveRenderDepth = [];
/**
* The logger factory.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;
protected static array $recursiveRenderDepth = [];
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, EntityDisplayRepositoryInterface $entityDisplayRepository, LoggerChannelFactoryInterface $logger_factory) {
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
protected EntityTypeManagerInterface $entityTypeManager,
EntityDisplayRepositoryInterface $entityDisplayRepository,
protected LoggerChannelFactoryInterface $loggerFactory,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
// Determine what entity type we are referring to.
$this->entityTypeName = $this->getDerivativeId();
// Load various utilities related to our entity type.
$this->entityTypeManager = $entityTypeManager;
$this->entityStorage = $entityTypeManager->getStorage($this->entityTypeName);
try {
// Determine what entity type we are referring to.
$this->entityTypeName = $this->getDerivativeId();
// Panelizer replaces the view_builder handler, but we want to use the
// original which has been moved to fallback_view_builder.
if ($entityTypeManager->hasHandler($this->entityTypeName, 'fallback_view_builder')) {
$this->entityViewBuilder = $entityTypeManager->getHandler($this->entityTypeName, 'fallback_view_builder');
}
else {
// Load various utilities related to our entity type.
$entityTypeManager = $this->entityTypeManager;
$this->entityStorage = $entityTypeManager->getStorage($this->entityTypeName);
$this->entityViewBuilder = $entityTypeManager->getHandler($this->entityTypeName, 'view_builder');
$this->viewModeOptions = $entityDisplayRepository->getViewModeOptions($this->entityTypeName);
}
catch (InvalidPluginDefinitionException | PluginNotFoundException $e) {
$this->loggerFactory->get('entity')->error($e->getMessage());
}
$this->view_mode_options = $entityDisplayRepository->getViewModeOptions($this->entityTypeName);
$this->loggerFactory = $logger_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
return new static(
$configuration, $plugin_id, $plugin_definition,
$container->get('entity_type.manager'),
......@@ -122,7 +117,7 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
public function blockForm($form, FormStateInterface $form_state): array {
$form = parent::blockForm($form, $form_state);
$config = $this->configuration;
......@@ -145,7 +140,7 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
$form['view_mode'] = [
'#type' => 'select',
'#title' => $this->t('View mode'),
'#options' => $this->view_mode_options,
'#options' => $this->viewModeOptions,
'#default_value' => $view_mode,
];
......@@ -155,7 +150,7 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$form = parent::buildConfigurationForm($form, $form_state);
// Hide default block form fields that are undesired in this case.
......@@ -172,7 +167,7 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
public function blockSubmit($form, FormStateInterface $form_state): void {
parent::blockSubmit($form, $form_state);
$this->configuration['entity'] = $form_state->getValue('entity');
......@@ -191,7 +186,7 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public function build() {
public function build(): array {
if ($entity = $this->getEntity()) {
$recursive_render_id = $entity->getEntityTypeId() . ':' . $entity->id();
if (isset(static::$recursiveRenderDepth[$recursive_render_id])) {
......@@ -207,8 +202,6 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
'%entity_type' => $entity->getEntityTypeId(),
'%entity_id' => $entity->id(),
]);
// return [];
}
$render_controller = $this->entityTypeManager->getViewBuilder($entity->getEntityTypeId());
......@@ -223,7 +216,7 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account) {
protected function blockAccess(AccountInterface $account): AccessResultInterface {
$entity = $this->getEntity();
if ($entity && $entity->access('view', $account)) {
return AccessResult::allowed()
......@@ -238,7 +231,7 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
public function getCacheContexts(): array {
$entity = $this->getEntity();
$contexts = $entity ? $entity->getCacheContexts() : [];
return Cache::mergeContexts(parent::getCacheContexts(), $contexts);
......@@ -247,7 +240,7 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public function getCacheTags() {
public function getCacheTags(): array {
$entity = $this->getEntity();
$cache_tags = $entity ? $entity->getCacheTags() : [];
return Cache::mergeTags(parent::getCacheTags(), $cache_tags);
......@@ -256,7 +249,7 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
public function getCacheMaxAge(): int {
$entity = $this->getEntity();
$max_age = $entity ? $entity->getCacheMaxAge() : Cache::PERMANENT;
return Cache::mergeMaxAges(parent::getCacheMaxAge(), $max_age);
......@@ -265,7 +258,7 @@ class EntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* Gets our entity.
*/
public function getEntity() {
public function getEntity(): ?EntityInterface {
if ($entity_id = $this->configuration['entity']) {
return $this->entityStorage->load($entity_id);
}
......
......@@ -2,16 +2,17 @@
namespace Drupal\entity_block\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides block plugin definitions for all Entity Block block displays.
* Provides block plugin definitions for all Entity Block displays.
*
* @see \Drupal\entity_block\Plugin\block\block\EntityBlock
* @see \Drupal\entity_block\Plugin\block\EntityBlock
*/
class EntityBlock implements ContainerDeriverInterface {
class EntityBlockDeriver extends DeriverBase implements ContainerDeriverInterface {
/**
* List of derivative definitions.
......@@ -21,42 +22,26 @@ class EntityBlock implements ContainerDeriverInterface {
protected $derivatives = [];
/**
* The base plugin ID.
*
* @var string
*/
protected $basePluginId;
/**
* The entity type manager.
* Constructs a EntityBlock object.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
* @param string $base_plugin_id
* The base plugin ID.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
*/
protected $entityTypeManager;
public function __construct(protected string $base_plugin_id, protected EntityTypeManagerInterface $entityTypeManager) {
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
public static function create(ContainerInterface $container, $base_plugin_id): static {
return new static(
$base_plugin_id,
$container->get('entity_type.manager')
);
}
/**
* Constructs a EntityBlock object.
*
* @param string $base_plugin_id
* The base plugin ID.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
*/
public function __construct($base_plugin_id, EntityTypeManagerInterface $entityTypeManager) {
$this->basePluginId = $base_plugin_id;
$this->entityTypeManager = $entityTypeManager;
}
/**
* {@inheritdoc}
*/
......@@ -71,7 +56,7 @@ class EntityBlock implements ContainerDeriverInterface {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition): array {
foreach ($this->entityTypeManager->getDefinitions() as $entityDefinition) {
if ($entityDefinition->hasViewBuilderClass()) {
$delta = $entityDefinition->id();
......@@ -80,7 +65,7 @@ class EntityBlock implements ContainerDeriverInterface {
'admin_label' => $entityDefinition->getLabel(),
'config_dependencies' => [
'config' => [
// TODO: Add proper config dependencies.
// @todo Add proper config dependencies.
],
],
];
......
......@@ -27,6 +27,11 @@ final class EntityBlockTest extends EntityKernelTestBase {
'entity_block',
];
/**
* Tests Entity block config schema.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function testBlockConfigSchema(): void {
$entity = EntityTest::create([
'name' => $this->randomString(),
......@@ -51,6 +56,12 @@ final class EntityBlockTest extends EntityKernelTestBase {
$this->assertConfigSchemaByName($block->getConfigDependencyName());
}
/**
* Test Entity block build.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* @throws \Exception
*/
public function testBuild(): void {
$entity = EntityTest::create([
'name' => $this->randomString(),
......@@ -81,7 +92,6 @@ final class EntityBlockTest extends EntityKernelTestBase {
$block_build = $block->getPlugin()->build();
unset($block_build['#entity_test']);
self::assertEquals($view_builder_build, $block_build);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment