diff --git a/src/Service/UWService.php b/src/Service/UWService.php index 936499528899fc13aeadcbab0d5c35a9a665fc3b..0643499d9a875429d844cb829af2768a44c69601 100644 --- a/src/Service/UWService.php +++ b/src/Service/UWService.php @@ -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; + } + } diff --git a/src/Service/UWServiceInterface.php b/src/Service/UWServiceInterface.php index 20893c63007b62057ba49e50093e3495e44bf791..f0c17c456875cfcecf0780fb3e913c748ff2a9bb 100644 --- a/src/Service/UWServiceInterface.php +++ b/src/Service/UWServiceInterface.php @@ -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; + } diff --git a/uw_cfg_common.services.yml b/uw_cfg_common.services.yml index 3ebdfbfa42b4619ed8997f9a3b08a3fee9be95a7..750c7773cb38eff8da0452bcbf2053ab2c0dcb94 100644 --- a/uw_cfg_common.services.yml +++ b/uw_cfg_common.services.yml @@ -1,7 +1,7 @@ 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: