Skip to content

Commit

Permalink
Merge pull request #21 from discoverygarden/fix/refactor-onto-upstream
Browse files Browse the repository at this point in the history
DGIR-71: Fix/refactor onto upstream
  • Loading branch information
nchiasson-dgi authored Dec 6, 2023
2 parents d8cbb53 + c0a28c1 commit c5e16c1
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 183 deletions.
4 changes: 2 additions & 2 deletions dgi_members.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* Contains dgi_members.module.
*/

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\views\ViewExecutable;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
use Drupal\views\ViewExecutable;

/**
* Implements hook_help().
Expand Down
7 changes: 7 additions & 0 deletions js/compound_parts.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@
*/
appendLabels: function () {
$(".compound-object-metadata, .compound-member-metadata").find('.panel-heading').append(
// XXX: Drupal's/Squiz coding standards do not presently appears to be
// aware of the possibility of template strings in Javascript.
// phpcs:disable Squiz.WhiteSpace.OperatorSpacing.NoSpaceBefore,Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter
`<span class='metadata-toggle'>
<a href='#' class='object-metadata part-metadata'>${Drupal.t("Part")}</a>
<a href='#' class='object-metadata element-compound'>${Drupal.t("Compound")}</a>
</span>`
// phpcs:enable Squiz.WhiteSpace.OperatorSpacing.NoSpaceBefore,Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter
);
},

Expand All @@ -101,6 +105,9 @@
$pm.removeClass(ac);
}

// XXX: Drupal's/Squiz coding standards do not presently appears to be
// aware of the possibility of template strings in Javascript.
// phpcs:ignore Squiz.WhiteSpace.OperatorSpacing.NoSpaceBefore,Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter
$(`.active-node-${drupalSettings.dgi_members.active_nid}`).closest('div.views-row').addClass('active-member');
}
};
Expand Down
64 changes: 26 additions & 38 deletions src/DgiMembersEntityOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Drupal\dgi_members;

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\islandora\IslandoraUtils;
use Drupal\node\NodeInterface;
use Drupal\taxonomy\TermInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Utility service to perform compound object related operations.
*/
class DgiMembersEntityOperations {
class DgiMembersEntityOperations implements DgiMembersEntityOperationsInterface {

/**
* The route match.
Expand Down Expand Up @@ -50,32 +50,24 @@ class DgiMembersEntityOperations {
protected RequestStack $requestStack;

/**
* DgiMembersEntityOperations constructor.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* Current route match.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager.
* @param \Drupal\islandora\IslandoraUtils $islandoraUtils
* Utility functions from islandora core.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* Request stack.
* Constructor.
*/
public function __construct(RouteMatchInterface $routeMatch, EntityTypeManagerInterface $entityTypeManager, IslandoraUtils $islandoraUtils, RequestStack $requestStack) {
public function __construct(
RouteMatchInterface $routeMatch,
EntityTypeManagerInterface $entityTypeManager,
IslandoraUtils $islandoraUtils,
RequestStack $requestStack,
) {
$this->routeMatch = $routeMatch;
$this->entityTypeManager = $entityTypeManager;
$this->islandoraUtils = $islandoraUtils;
$this->requestStack = $requestStack;
}

/**
* Confirm the routeMatch node parameter has the 'Compound Object' term.
*
* @return bool
* TRUE if the current node in the route is a compound object, FALSE
* otherwise.
* {@inheritDoc}
*/
public function nodeFromRouteIsCompound() {
public function nodeFromRouteIsCompound() : bool {
$entity = $this->routeMatch->getParameter('node');
if ($entity instanceof NodeInterface) {
if ($entity->hasField($this->islandoraUtils::MODEL_FIELD) && !$entity->get($this->islandoraUtils::MODEL_FIELD)->isEmpty()) {
Expand Down Expand Up @@ -114,15 +106,13 @@ public function nodeFromRouteIsCompound() {
}

/**
* Retrieve the first member of the given object or the node from url param.
*
* @return bool|NodeInterface
* FALSE if unable to retrieve an active member, or the member if present.
* {@inheritDoc}
*/
public function retrieveActiveMember($url_param = NULL) {
public function retrieveActiveMember($url_param = NULL) : FALSE|NodeInterface {
if ($url_param) {
$active_member_param = $this->requestStack->getCurrentRequest()->query->get($url_param);
if ($active_member_param) {
/** @var \Drupal\node\NodeInterface $active_member */
$active_member = $this->entityTypeManager->getStorage('node')->load($active_member_param);
if ($active_member) {
return $active_member;
Expand All @@ -136,28 +126,26 @@ public function retrieveActiveMember($url_param = NULL) {
}

/**
* Retrieve the first member of the contextual 'Node'.
*
* @return bool|NodeInterface
* FALSE if the member could not be retrieved, or the member object.
* {@inheritDoc}
*/
public function retrieveFirstOfMembers() {
$nodes = $this->membersQueryExecute();
public function retrieveFirstOfMembers() : FALSE|NodeInterface {
$node_ids = $this->membersQueryExecute();

if (empty($nodes)) {
if (empty($node_ids)) {
return FALSE;
}

return $this->entityTypeManager->getStorage('node')->load(reset($nodes));
$node_id = reset($node_ids);
/** @var \Drupal\node\NodeInterface $node */
$node = $this->entityTypeManager->getStorage('node')->load($node_id);

return $node;
}

/**
* Retrieve 'members' of the current page object.
*
* @return bool|array
* An array of members for the given page object, or FALSE if none found.
* {@inheritDoc}
*/
public function membersQueryExecute() {
public function membersQueryExecute() : array|FALSE {
$entity = $this->routeMatch->getParameter('node');

if (!$entity) {
Expand Down
45 changes: 45 additions & 0 deletions src/DgiMembersEntityOperationsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Drupal\dgi_members;

use Drupal\node\NodeInterface;

/**
* Member operations service interface.
*/
interface DgiMembersEntityOperationsInterface {

/**
* Confirm the routeMatch node parameter has the 'Compound Object' term.
*
* @return bool
* TRUE if the current node in the route is a compound object, FALSE
* otherwise.
*/
public function nodeFromRouteIsCompound() : bool;

/**
* Retrieve the first member of the given object or the node from url param.
*
* @return false|\Drupal\node\NodeInterface
* FALSE if unable to retrieve an active member, or the member if present.
*/
public function retrieveActiveMember($url_param = NULL) : FALSE|NodeInterface;

/**
* Retrieve the first member of the contextual 'Node'.
*
* @return false|\Drupal\node\NodeInterface
* FALSE if the member could not be retrieved, or the member object.
*/
public function retrieveFirstOfMembers() : FALSE|NodeInterface;

/**
* Retrieve 'members' of the current page object.
*
* @return false|string[]|int[]
* An array of member IDs for the given object, or FALSE if none found.
*/
public function membersQueryExecute() : array|FALSE;

}
Loading

0 comments on commit c5e16c1

Please sign in to comment.