Commit d80d46ca authored by kbrodej's avatar kbrodej Committed by boshtian

Issue #3109919 by kbrodej, strozx, Suresh Prabhu Parkala, boshtian, paulocs,...

Issue #3109919 by kbrodej, strozx, Suresh Prabhu Parkala, boshtian, paulocs, Sutharsan: Reaction to change the page title
parent 67d11dbe
<?php
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Utility\NestedArray;
......@@ -39,6 +40,22 @@ function context_preprocess_html(&$variables) {
foreach ($context_manager->getActiveReactions('body_class') as $reaction) {
$variables['attributes'] = NestedArray::mergeDeep($variables['attributes'], $reaction->execute());
}
foreach ($context_manager->getActiveReactions('page_title') as $reaction) {
$variables['head_title']['title'] = Markup::create(trim(strip_tags($reaction->execute())));
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function context_preprocess_page_title(&$variables) {
/** @var \Drupal\context\ContextManager $context_manager */
$context_manager = \Drupal::service('context.manager');
foreach ($context_manager->getActiveReactions('page_title') as $reaction) {
$variables['title'] = $reaction->execute();
}
}
/**
......
<?php
namespace Drupal\context\Plugin\ContextReaction;
use Drupal\context\ContextReactionPluginBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a reaction that changes page title.
*
* @ContextReaction(
* id = "page_title",
* label = @Translation("Page title")
* )
*/
class PageTitle extends ContextReactionPluginBase {
/**
* {@inheritDoc}
*/
public function summary() {
return $this->t('Lets you override the page title');
}
/**
* {@inheritDoc}
*/
public function execute() {
$config = $this->getConfiguration();
return $config['page_title'];
}
/**
* {@inheritDoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$config = $this->getConfiguration();
$form['page_title'] = [
'#type' => 'textfield',
'#title' => $this->t('Page title'),
'#default_value' => isset($config['page_title']) ? $config['page_title'] : '',
'#description' => $this->t('Enter the title you wish to display.'),
];
return $form;
}
/**
* {@inheritDoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$config['page_title'] = $form_state->getValue('page_title');
$this->setConfiguration($config);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment