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

Merge branch 'feature/ISTWCMS-4174-ebremner-cleanup-sidebar-code' into '8.x-1.x'

Feature/istwcms 4174 ebremner cleanup sidebar code

See merge request !11
parents fc279232 9081189f
No related branches found
No related tags found
1 merge request!11Feature/istwcms 4174 ebremner cleanup sidebar code
......@@ -3,7 +3,9 @@
namespace Drupal\uw_cfg_common\Service;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Database\Connection;
/**
* Class UWService.
......@@ -14,6 +16,33 @@ use Drupal\node\NodeInterface;
*/
class UWService implements UWServiceInterface {
/**
* Entity type manager from core.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private $entityTypeManager;
/**
* Database connection.
*
* @var \Drupal\Core\Database\Connection
*/
private $database;
/**
* Default constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity Type Manager from core.
* @param \Drupal\Core\Database\Connection $database
* The database entity.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database) {
$this->entityTypeManager = $entityTypeManager;
$this->database = $database;
}
/**
* {@inheritDoc}
*/
......@@ -158,4 +187,76 @@ class UWService implements UWServiceInterface {
return $variables;
}
/**
* {@inheritDoc}
*/
public function getOrCheckAttachedSidebar(int $attached_page_nid, int $sidebar_nid = NULL, string $type = NULL): int {
// A database called to get the field with the condition of the
// attached_page_nid.
$result = $this->database
->select('node__field_uw_attach_page', 'nfuap')
->fields('nfuap', ['entity_id', 'field_uw_attach_page_target_id'])
->condition('field_uw_attach_page_target_id', $attached_page_nid);
// If we are doing a a check for sidebar, then add the condition
// that it is not equal to the current sidebar_nid that we are on.
// Meaning that we are checking for any other sidebars that this
// attached_page_nid is attached to.
if ($type == 'check') {
// Add the not equals to condition.
$result->condition('entity_id', $sidebar_nid, '<>');
}
// Get the results of query, we only need to fetch, because we are
// only every expecting one sidebar only has one other node page
// attached.
$results = $result->execute()->fetch();
// If we have results, then return the entity_id, which is the
// sidebar_nid that the attached_page_nid is attached to.
if (isset($results->entity_id)) {
// Return the entity_id.
return $results->entity_id;
}
// If we made it here, there are no other sidebars that the
// attached_page_nid is attached to, so we can simply return 0.
else {
return 0;
}
}
/**
* {@inheritDoc}
*/
public function getUwContentTypes($with_sidebar = FALSE): array {
$return_content_types = [];
// This is the list of all UW content types, with a TRUE/FALSE, indicating
// whether or not a sidebar can be attached to this content type.
$content_types = [
'uw_ct_blog' => TRUE,
'uw_ct_catalog_item' => FALSE,
'uw_ct_event' => TRUE,
'uw_ct_news_item' => TRUE,
'uw_ct_sidebar' => FALSE,
'uw_ct_site_footer' => FALSE,
'uw_ct_web_page' => TRUE,
];
foreach ($content_types as $key => $value) {
if ($with_sidebar && $value) {
$return_content_types[] = $key;
}
elseif (!$with_sidebar) {
$return_content_types[] = $key;
}
}
return $return_content_types;
}
}
......@@ -42,4 +42,45 @@ interface UWServiceInterface {
*/
public function getTeaserContent(NodeInterface $node, array $variables_to_get, string $teaser_type): array;
/**
* A function to get or check the attached sidebar.
*
* If the type is set to check, then the function will return one
* of two things. It will either return a 0, which means that the
* attached_page_nid is not attached to any other sidebar in the
* system. Or it will return the entity_id of the sidebar (sidebar_nid)
* that it is currently attached to.
*
* For example:
* If the attached_page_nid is 3 and the sidebar_nid is 7, and
* the query finds that 3 is already attached to sidebar_nid 6, then 6
* will be returned.
*
* If the attached_page_nid is 3 and the sidebar_nid is 7, and
* the query finds no other sidebar_nids that 3 is attached to, then 0
* will be returned.
*
* @param int $attached_page_nid
* An integer value that represents the nid of the page to be attached to.
* @param int|null $sidebar_nid
* An integer value that represents the nid of the current sidebar.
* @param string|null $type
* A string value that represents either "get" or "check".
*
* @return int
* A value that is either 0 (no nids) or the entity_id of a sidebar.
*/
public function getOrCheckAttachedSidebar(int $attached_page_nid, int $sidebar_nid = NULL, string $type = NULL): int;
/**
* A function to get an array of UW content types, with or without sidebar.
*
* @param bool $with_sidebar
* A boolean to state if we want content types that can have sidebars.
*
* @return array
* An array of the machine names of the UW content types.
*/
public function getUwContentTypes(bool $with_sidebar = FALSE): array;
}
services:
uw_cfg_common.uw_service:
class: Drupal\uw_cfg_common\Service\UWService
arguments: []
arguments: ['@entity_type.manager', '@database']
uw_cfg_common.route_subscriber:
class: Drupal\uw_cfg_common\Routing\UwNodeAccessRouteSubscriber
tags:
......
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