Refactor to use a Post class

References #78
This commit is contained in:
Oliver Davies 2020-05-29 21:35:21 +01:00
parent 79eb9bef0b
commit 165b6e8195
7 changed files with 88 additions and 7 deletions

View file

@ -7,14 +7,15 @@
declare(strict_types=1);
use Drupal\custom\Entity\Node;
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']->setClass(Node::class);
$entityTypes['node']->setStorageClass(NodeStorage::class);
}
}

View file

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Drupal\custom\Entity\Node;
use Drupal\node\Entity\Node;
use Drupal\discoverable_entity_bundle_classes\ContentEntityBundleInterface;
/**
* Defines an blog post node class.
*
* @ContentEntityBundleClass(
* label = @Translation("Blog post"),
* entity_type = "node",
* bundle = "post"
* );
*/
class Post extends Node implements ContentEntityBundleInterface {
public function hasTweet(): bool {
return (bool) $this->get('field_has_tweet')->getString();
}
}

View file

@ -22,6 +22,7 @@ abstract class TalksTestBase extends EntityKernelTestBase {
'datetime',
// Contrib.
'discoverable_entity_bundle_classes',
'entity_reference_revisions',
'paragraphs',
'hook_event_dispatcher',

View file

@ -5,20 +5,22 @@
* Functions to support theming in the Tailwind CSS theme.
*/
use Drupal\custom\Entity\Node\Post;
/**
* Implements hook_preprocess_HOOK().
*/
function opdavies_preprocess_page(array &$variables): void {
/** @var \Drupal\Core\Entity\EntityInterface $node */
if (!$node = \Drupal::routeMatch()->getParameter('node')) {
/** @var \Drupal\custom\Entity\Node\Post $node */
if (!$node = $variables['node']) {
return;
}
if ($node->getType() != 'post') {
if (!$node instanceof Post) {
return;
}
if ($node->get('field_has_tweet')->getString()) {
if ($node->hasTweet()) {
$variables['#attached']['library'][] = 'opdavies/twitter';
}
}