Skip to content

Commit

Permalink
feat: Replace title with new title and metadata block and layout for …
Browse files Browse the repository at this point in the history
…posts
  • Loading branch information
codechefmarc committed Jul 21, 2023
1 parent 435ae37 commit 0d1ec68
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies:
- layout_builder_restrictions
- layout_discovery
- user
- ys_layouts
third_party_settings:
layout_builder:
enabled: true
Expand All @@ -27,9 +28,20 @@ third_party_settings:
-
layout_id: layout_onecol
layout_settings:
label: 'Title Section'
label: 'Title and Metadata'
context_mapping: { }
components: { }
components:
6abf6a0c-ae3d-47f8-bf30-08e382c90124:
uuid: 6abf6a0c-ae3d-47f8-bf30-08e382c90124
region: content
configuration:
id: post_meta_block
label: 'Post Meta Block'
label_display: ''
provider: ys_layouts
context_mapping: { }
weight: 0
additional: { }
third_party_settings:
layout_builder_lock:
lock:
Expand All @@ -39,6 +51,7 @@ third_party_settings:
4: 4
5: 5
6: 6
7: 7
8: 8
-
layout_id: layout_onecol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TestPageController extends ControllerBase {
public function testPage() {

$updateExistingNodes = new UpdateExistingNodes();
$updateExistingNodes->updateExistingPageLock();
$updateExistingNodes->updateExistingPostMeta();

return [
'#markup' => 'Hello, world',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public function build() {
// Get the page title.
if ($route) {
$page_title = $this->titleResolver->getTitle($request, $route);

};

return [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Drupal\ys_layouts\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Controller\TitleResolver;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\node\NodeInterface;

/**
* Block for post meta data that appears above posts.
*
* @Block(
* id = "post_meta_block",
* admin_label = @Translation("Post Meta Block"),
* category = @Translation("YaleSites Layouts"),
* )
*/
class PostMetaBlock extends BlockBase implements ContainerFactoryPluginInterface {

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

/**
* The current route match.
*
* @var \Drupal\Core\Controller\TitleResolver
*/
protected $titleResolver;

/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;

/**
* Constructs a new YaleSitesBreadcrumbBlock 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\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\Core\Controller\TitleResolver $title_resolver
* The title resolver.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, TitleResolver $title_resolver, RequestStack $request_stack) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $route_match;
$this->titleResolver = $title_resolver;
$this->requestStack = $request_stack;
}

/**
* {@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'),
$container->get('title_resolver'),
$container->get('request_stack'),
);
}

/**
* {@inheritdoc}
*/
public function build() {

/** @var \Drupal\node\NodeInterface $node */
$node = $this->routeMatch->getParameter('node');
if (!($node instanceof NodeInterface)) {
return [];
}

// Post fields.
$title = $node->getTitle();
$publishDate = strtotime($node->field_publish_date->first()->getValue()['value']);
$dateFormatted = \Drupal::service('date.formatter')->format($publishDate, '', 'c');
return [
'#theme' => 'ys_post_meta_block',
'#label' => $title,
'#date_formatted' => $dateFormatted,
];
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,60 @@ public function updateExistingPageLock() {
}
}

/**
* Updates Post Meta for existing nodes.
*/
public function updateExistingPostMeta() {

// Gets the main page meta section to clone.
$entityTypeManager = \Drupal::service('entity_type.manager');
$postMetaSection = NULL;

if ($postViewDisplay = $entityTypeManager->getStorage('entity_view_display')->load('node.post.default')) {
if ($postViewDisplay->isLayoutBuilderEnabled()) {
$postSections = $postViewDisplay->getSections();
foreach ($postSections as $section) {
if ($section->getLayoutSettings()['label'] == 'Title and Metadata') {
$postMetaSection = $section;
}
}
}
}

if ($postMetaSection instanceof Section) {

// Find all post nodes to update existing.
$nids = \Drupal::entityQuery('node')->condition('type', 'post')->execute();

foreach ($nids as $nid) {
$node = Node::load($nid);
$layout = $node->get('layout_builder__layout');
/** @var \Drupal\layout_builder\Field\LayoutSectionItemList $layout */
$sections = $layout->getSections();

foreach ($sections as $section) {
// If an overridden layout already contains an Page Meta section,
// remove it from the update list.
if ($section->getLayoutSettings()['label'] == 'Title and Metadata') {
unset($nids[array_search($nid, $nids)]);
}
}
}

foreach ($nids as $nid) {
$node = Node::load($nid);
$layout = $node->get('layout_builder__layout');

$section_storage = $this->getSectionStorageForEntity($node);
$tempStore = \Drupal::service('layout_builder.tempstore_repository');
/** @var \Drupal\layout_builder\Field\LayoutSectionItemList $layout */
// For existing pages, remove the old title and breadcrumb block first.
$layout->removeSection(0);
$layout->insertSection(0, $postMetaSection);
$tempStore->set($section_storage);
$node->save();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% include "@molecules/page-title/yds-page-title.twig" with {
page_title__heading: label,
page_title__meta: content.field_author|render ~ date_formatted|date("l, F j, Y"),
page_title__width: 'content',
} %}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ function ys_layouts_deploy_9001() {

// Adds "Add section" to existing pages.
$updateExistingNodes->updateExistingPageLock();

// Replaces old title section with new post meta block.
$updateExistingNodes->updateExistingPostMeta();
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ function ys_layouts_theme($existing, $type, $theme, $path): array {
'page_title_display' => 'visible',
],
],
'ys_post_meta_block' => [
'variables' => [
'label' => NULL,
'date_formatted' => NULL,
],
],
];
}

0 comments on commit 0d1ec68

Please sign in to comment.