Skip to content
Snippets Groups Projects
Commit cb2cfa02 authored by Igor Biki's avatar Igor Biki
Browse files

Merge branch 'feature/ISTWCMS-4092-ebremner-sidebar' into '8.x-1.x'

ISTWCMS-4092: adding the sidebar build array to the node in preprocess

See merge request !1
parents e3b99fc7 6d990bee
No related branches found
No related tags found
1 merge request!1ISTWCMS-4092: adding the sidebar build array to the node in preprocess
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
/**
* Implements hook_form_alter().
*
*/
function uw_ct_sidebar_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
switch ($form_id) {
case 'node_uw_ct_sidebar_form':
case 'node_uw_ct_sidebar_edit_form':
// Hide title when creating a new sidebar node.
$form['title']['#access'] = FALSE;
// Set a title when creating a new sidebar node.
$form['#entity_builders'][] = 'uw_ct_sidebar_node_builder';
// Add the validation for field_uw_attach_page.
$form['#validate'][] = 'uw_ct_sidebar_field_uw_attach_page_validate';
break;
case 'node_uw_ct_blog_layout_builder_form':
case 'node_uw_ct_event_layout_builder_form':
case 'node_uw_ct_news_item_layout_builder_form':
case 'node_uw_ct_web_page_layout_builder_form':
case 'node_uw_ct_blog_edit_form':
case 'node_uw_ct_event_edit_form':
case 'node_uw_ct_news_item_edit_form':
case 'node_uw_ct_web_page_edit_form':
// Get the loading node
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
// Display message.
uw_ct_sidebar_add_message_for_attached_page($node->id());
}
break;
}
}
/**
* The custom function to set title of sidebar to be the title of the node reference.
*/
function uw_ct_sidebar_node_builder($entity_type, NodeInterface $node, $form, FormStateInterface $form_state) {
// Get the title of the node reference.
if ($node->field_uw_attach_page->entity) {
$title_of_attach_to_page = $node->field_uw_attach_page->entity->getTitle();;
// Set the title of sidebar to be the title of the node reference.
$node->setTitle($title_of_attach_to_page);
}
}
/**
* Form validation handler invoked by uw_ct_sidebar_form_alter.
*
* Ensure that the value of attach to page is NOT referenced by others for both node/add and node/nid/edit.
*/
function uw_ct_sidebar_field_uw_attach_page_validate($form, FormStateInterface &$form_state) {
$values = $form_state->getValues();
if (!empty($values['field_uw_attach_page'][0]['target_id'])) {
// Get the reference node id.
$attached_page_nid = $values['field_uw_attach_page'][0]['target_id'];
// Sidebar node path is like '/node/100'.
$sidebar_node_path = $values['path'][0]['source'];
// Get node id from sidebar node path.
$sidebar_nid = substr($sidebar_node_path, 6);
// Display error message.
uw_ct_sidebar_prepare_sidebar_validate_message($form_state, $attached_page_nid, $sidebar_nid);
}
}
/**
* @param $attached_page_nid
* @param $current_sidebar_nid
* @return \Drupal\Core\GeneratedLink|string
*
* The helper function to prepare a message for sidebar validation.
*/
function uw_ct_sidebar_prepare_sidebar_validate_message(&$form_state, $attached_page_nid, $current_sidebar_nid){
// Get both attached page and sidebar nid from database table node__field_uw_attach_page.
$results = \Drupal::database()->select('node__field_uw_attach_page', 'nfuap')
->fields('nfuap', ['entity_id', 'field_uw_attach_page_target_id'])
->execute()->fetchAll();
foreach ($results as $result) {
// If the given attached page node id is in node__field_uw_attached_page.
if ($attached_page_nid == $result->field_uw_attach_page_target_id) {
// If the given sidebar node is NOT the matching sidebar node id.
// $result->field_uw_attach_page_target_id and $result->entity_id are pair.
if (!($current_sidebar_nid == $result->entity_id)) {
$url = Url::fromRoute('entity.node.canonical', array('node' => $result->entity_id));
$args = array(
'@text_with_link' => Link::fromTextAndUrl('View the existing sidebar.', $url)->toString(),
);
$form_state->setErrorByName('field_uw_attach_page', t('The selected page already has a sidebar attached. @text_with_link', $args));
break;
}
}
}
}
/**
* @param $current_page_nid
* @return \Drupal\Core\GeneratedLink|string
*
* Prepare message for attached page.
*/
function uw_ct_sidebar_add_message_for_attached_page($current_page_nid){
// Get both attached page and sidebar nid from database table node__field_uw_attach_page.
$results = \Drupal::database()->select('node__field_uw_attach_page', 'nfuap')
->fields('nfuap', ['entity_id', 'field_uw_attach_page_target_id'])
->execute()->fetchAll();
foreach ($results as $result) {
// If the given attached page node id is in node__field_uw_attached_page,
// add message to the attached page.
if ($current_page_nid == $result->field_uw_attach_page_target_id) {
$url = Url::fromRoute('entity.node.canonical', array('node' => $result->entity_id));
$args = array(
'@text_with_link' => Link::fromTextAndUrl('View the sidebar.', $url)->toString(),
);
\Drupal::messenger()->addMessage(t('This page has a sidebar attached. @text_with_link', $args));
break;
}
}
}
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\node\Entity\Node;
/**
* Implements hook_form_alter().
*
*/
function uw_ct_sidebar_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
switch ($form_id) {
case 'node_uw_ct_sidebar_form':
case 'node_uw_ct_sidebar_edit_form':
// Hide title when creating a new sidebar node.
$form['title']['#access'] = FALSE;
// Set a title when creating a new sidebar node.
$form['#entity_builders'][] = 'uw_ct_sidebar_node_builder';
// Add the validation for field_uw_attach_page.
$form['#validate'][] = 'uw_ct_sidebar_field_uw_attach_page_validate';
break;
case 'node_uw_ct_blog_layout_builder_form':
case 'node_uw_ct_event_layout_builder_form':
case 'node_uw_ct_news_item_layout_builder_form':
case 'node_uw_ct_web_page_layout_builder_form':
case 'node_uw_ct_blog_edit_form':
case 'node_uw_ct_event_edit_form':
case 'node_uw_ct_news_item_edit_form':
case 'node_uw_ct_web_page_edit_form':
// Get the loading node
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
// Display message.
uw_ct_sidebar_add_message_for_attached_page($node->id());
}
break;
}
}
/**
* The custom function to set title of sidebar to be the title of the node reference.
*/
function uw_ct_sidebar_node_builder($entity_type, NodeInterface $node, $form, FormStateInterface $form_state) {
// Get the title of the node reference.
if ($node->field_uw_attach_page->entity) {
$title_of_attach_to_page = $node->field_uw_attach_page->entity->getTitle();;
// Set the title of sidebar to be the title of the node reference.
$node->setTitle($title_of_attach_to_page);
}
}
/**
* Form validation handler invoked by uw_ct_sidebar_form_alter.
*
* Ensure that the value of attach to page is NOT referenced by others for both node/add and node/nid/edit.
*/
function uw_ct_sidebar_field_uw_attach_page_validate($form, FormStateInterface &$form_state) {
$values = $form_state->getValues();
if (!empty($values['field_uw_attach_page'][0]['target_id'])) {
// Get the reference node id.
$attached_page_nid = $values['field_uw_attach_page'][0]['target_id'];
// Sidebar node path is like '/node/100'.
$sidebar_node_path = $values['path'][0]['source'];
// Get node id from sidebar node path.
$sidebar_nid = substr($sidebar_node_path, 6);
// Display error message.
uw_ct_sidebar_prepare_sidebar_validate_message($form_state, $attached_page_nid, $sidebar_nid);
}
}
/**
* @param $attached_page_nid
* @param $current_sidebar_nid
* @return \Drupal\Core\GeneratedLink|string
*
* The helper function to prepare a message for sidebar validation.
*/
function uw_ct_sidebar_prepare_sidebar_validate_message(&$form_state, $attached_page_nid, $current_sidebar_nid){
// Get both attached page and sidebar nid from database table node__field_uw_attach_page.
$results = \Drupal::database()->select('node__field_uw_attach_page', 'nfuap')
->fields('nfuap', ['entity_id', 'field_uw_attach_page_target_id'])
->execute()->fetchAll();
foreach ($results as $result) {
// If the given attached page node id is in node__field_uw_attached_page.
if ($attached_page_nid == $result->field_uw_attach_page_target_id) {
// If the given sidebar node is NOT the matching sidebar node id.
// $result->field_uw_attach_page_target_id and $result->entity_id are pair.
if (!($current_sidebar_nid == $result->entity_id)) {
$url = Url::fromRoute('entity.node.canonical', array('node' => $result->entity_id));
$args = array(
'@text_with_link' => Link::fromTextAndUrl('View the existing sidebar.', $url)->toString(),
);
$form_state->setErrorByName('field_uw_attach_page', t('The selected page already has a sidebar attached. @text_with_link', $args));
break;
}
}
}
}
/**
* @param $current_page_nid
* @return \Drupal\Core\GeneratedLink|string
*
* Prepare message for attached page.
*/
function uw_ct_sidebar_add_message_for_attached_page($current_page_nid){
// Get both attached page and sidebar nid from database table node__field_uw_attach_page.
$results = \Drupal::database()->select('node__field_uw_attach_page', 'nfuap')
->fields('nfuap', ['entity_id', 'field_uw_attach_page_target_id'])
->execute()->fetchAll();
foreach ($results as $result) {
// If the given attached page node id is in node__field_uw_attached_page,
// add message to the attached page.
if ($current_page_nid == $result->field_uw_attach_page_target_id) {
$url = Url::fromRoute('entity.node.canonical', array('node' => $result->entity_id));
$args = array(
'@text_with_link' => Link::fromTextAndUrl('View the sidebar.', $url)->toString(),
);
\Drupal::messenger()->addMessage(t('This page has a sidebar attached. @text_with_link', $args));
break;
}
}
}
/**
* Implements hook_preprocess_node().
*/
function uw_ct_sidebar_preprocess_node(&$variables) {
// The node types that the sidebar can be attached to.
$node_types = ['uw_ct_web_page', 'uw_ct_blog', 'uw_ct_news_item', 'uw_ct_event'];
// Check if the node we are in can have sidebars.
if (in_array($variables['node']->getType(), $node_types)) {
// Get both attached page and sidebar nid from database table node__field_uw_attach_page.
$results = \Drupal::database()->select('node__field_uw_attach_page', 'nfuap')
->fields('nfuap', ['entity_id', 'field_uw_attach_page_target_id'])
->execute()->fetchAll();
// Step through each of the results and see if we are to put it on the node.
foreach ($results as $result) {
// If the nid and the attach page target id from sidebar are the same,
// set the build array for the sidebar in the variables.
if ($variables['node']->nid->value == $result->field_uw_attach_page_target_id) {
// Get the node builder object.
$builder = \Drupal::entityTypeManager()->getViewBuilder('node');
// Get the node storage object.
$storage = \Drupal::entityTypeManager()->getStorage('node');
// Load the node from the entity_id from the sidebar.
$node = $storage->load($result->entity_id);
// Ensure that the sidebar node is published.
if ($node->status->value == 1) {
// Get the build array.
$build = $builder->view($node, 'default');
// Set the build array in the variables, so that the sidebar can be displayed.
$variables['sidebar'] = $build;
// Break out of the loop, so that we save computational time.
break;
}
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment