Split hooks into separate files, load with Finder

This commit is contained in:
Oliver Davies 2020-05-29 22:35:33 +01:00
parent fafe64dac7
commit c25eaf4ee1
3 changed files with 44 additions and 18 deletions

View file

@ -7,24 +7,12 @@
declare(strict_types=1);
use Drupal\discoverable_entity_bundle_classes\Storage\Node\NodeStorage;
use Symfony\Component\Finder\Finder;
/**
* Implements hook_entity_type_build().
*/
function custom_entity_type_build(array &$entityTypes): void {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entityTypes */
if (isset($entityTypes['node'])) {
$entityTypes['node']->setStorageClass(NodeStorage::class);
}
}
$finder = Finder::create()
->in(__DIR__ . DIRECTORY_SEPARATOR . 'hooks')
->name('/.[php|inc]$/');
/**
* Implements hook_preprocess_HOOK().
*/
function custom_preprocess_block(array &$variables): void {
// Add the 'markup' class to blocks.
if (in_array($variables['plugin_id'], ['views_block:featured_blog_posts-block_1'])) {
$variables['attributes']['class'][] = 'markup';
}
foreach ($finder as $file) {
include $file->getPathname();
}

View file

@ -0,0 +1,20 @@
<?php
/**
* @file
* Entity type build hooks.
*/
declare(strict_types=1);
use Drupal\discoverable_entity_bundle_classes\Storage\Node\NodeStorage;
/**
* Implements hook_entity_type_build().
*/
function custom_entity_type_build(array &$entityTypes): void {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entityTypes */
if (isset($entityTypes['node'])) {
$entityTypes['node']->setStorageClass(NodeStorage::class);
}
}

View file

@ -0,0 +1,18 @@
<?php
/**
* @file
* Block preprocess hooks.
*/
declare(strict_types=1);
/**
* Implements hook_preprocess_HOOK().
*/
function custom_preprocess_block(array &$variables): void {
// Add the 'markup' class to blocks.
if (in_array($variables['plugin_id'], ['views_block:featured_blog_posts-block_1'])) {
$variables['attributes']['class'][] = 'markup';
}
}