45 changed files with 1173 additions and 215 deletions
@ -1,13 +0,0 @@ |
|||||||
langcode: en |
|
||||||
status: true |
|
||||||
dependencies: |
|
||||||
enforced: |
|
||||||
module: |
|
||||||
- islandora |
|
||||||
module: |
|
||||||
- islandora |
|
||||||
id: delete_media |
|
||||||
label: 'Delete media' |
|
||||||
type: media |
|
||||||
plugin: delete_media |
|
||||||
configuration: { } |
|
||||||
@ -0,0 +1,3 @@ |
|||||||
|
maxDepth: -1 |
||||||
|
includeSelf: FALSE |
||||||
|
referenceField: field_member_of |
||||||
@ -0,0 +1,12 @@ |
|||||||
|
islandora.breadcrumbs: |
||||||
|
type: config_object |
||||||
|
mapping: |
||||||
|
maxDepth: |
||||||
|
type: integer |
||||||
|
label: 'Max Depth' |
||||||
|
includeSelf: |
||||||
|
type: boolean |
||||||
|
label: 'Include Self' |
||||||
|
referenceField: |
||||||
|
type: string |
||||||
|
label: 'Reference Field' |
||||||
@ -0,0 +1,7 @@ |
|||||||
|
name: 'Islandora Breadcrumbs' |
||||||
|
type: module |
||||||
|
description: 'Builds breadcrumbs based on field_member_of relationships.' |
||||||
|
core: 8.x |
||||||
|
package: Islandora |
||||||
|
dependencies: |
||||||
|
- islandora |
||||||
@ -0,0 +1,6 @@ |
|||||||
|
services: |
||||||
|
islandora_breadcrumbs.breadcrumb: |
||||||
|
class: Drupal\islandora_breadcrumbs\IslandoraBreadcrumbBuilder |
||||||
|
arguments: ['@config.factory'] |
||||||
|
tags: |
||||||
|
- { name: breadcrumb_builder, priority: 100 } |
||||||
@ -0,0 +1,96 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora_breadcrumbs; |
||||||
|
|
||||||
|
use Drupal\Core\Config\ConfigFactoryInterface; |
||||||
|
use Drupal\Core\Entity\EntityInterface; |
||||||
|
use Drupal\Core\Breadcrumb\Breadcrumb; |
||||||
|
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface; |
||||||
|
use Drupal\Core\Routing\RouteMatchInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Provides breadcrumbs for nodes using a configured entity reference field. |
||||||
|
*/ |
||||||
|
class IslandoraBreadcrumbBuilder implements BreadcrumbBuilderInterface { |
||||||
|
|
||||||
|
/** |
||||||
|
* The configuration. |
||||||
|
* |
||||||
|
* @var \Drupal\Core\Config\ImmutableConfig |
||||||
|
*/ |
||||||
|
protected $config; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs a breadcrumb builder. |
||||||
|
* |
||||||
|
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
||||||
|
* The configuration factory. |
||||||
|
*/ |
||||||
|
public function __construct(ConfigFactoryInterface $config_factory) { |
||||||
|
$this->config = $config_factory->get('islandora.breadcrumbs'); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function applies(RouteMatchInterface $attributes) { |
||||||
|
$parameters = $attributes->getParameters()->all(); |
||||||
|
if (!empty($parameters['node'])) { |
||||||
|
return ($parameters['node']->hasField($this->config->get('referenceField')) && |
||||||
|
!$parameters['node']->get($this->config->get('referenceField'))->isEmpty()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function build(RouteMatchInterface $route_match) { |
||||||
|
|
||||||
|
$node = $route_match->getParameter('node'); |
||||||
|
$breadcrumb = new Breadcrumb(); |
||||||
|
|
||||||
|
$chain = []; |
||||||
|
$this->walkMembership($node, $chain); |
||||||
|
|
||||||
|
if (!$this->config->get('includeSelf')) { |
||||||
|
array_pop($chain); |
||||||
|
} |
||||||
|
$breadcrumb->addCacheableDependency($node); |
||||||
|
|
||||||
|
// Add membership chain to the breadcrumb. |
||||||
|
foreach ($chain as $chainlink) { |
||||||
|
$breadcrumb->addCacheableDependency($chainlink); |
||||||
|
$breadcrumb->addLink($chainlink->toLink()); |
||||||
|
} |
||||||
|
$breadcrumb->addCacheContexts(['route']); |
||||||
|
return $breadcrumb; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Follows chain of field_member_of links. |
||||||
|
* |
||||||
|
* We pass crumbs by reference to enable checking for looped chains. |
||||||
|
*/ |
||||||
|
protected function walkMembership(EntityInterface $entity, &$crumbs) { |
||||||
|
// Avoid infinate loops, return if we've seen this before. |
||||||
|
foreach ($crumbs as $crumb) { |
||||||
|
if ($crumb->uuid == $entity->uuid) { |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Add this item onto the pile. |
||||||
|
array_unshift($crumbs, $entity); |
||||||
|
|
||||||
|
if ($this->config->get('maxDepth') > 0 && count($crumbs) >= $this->config->get('maxDepth')) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
// Find the next in the chain, if there are any. |
||||||
|
if ($entity->hasField($this->config->get('referenceField')) && |
||||||
|
!$entity->get($this->config->get('referenceField'))->isEmpty()) { |
||||||
|
$this->walkMembership($entity->get($this->config->get('referenceField'))->entity, $crumbs); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,109 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\Tests\islandora_breadcrumbs\Functional; |
||||||
|
|
||||||
|
use Drupal\Tests\islandora\Functional\IslandoraFunctionalTestBase; |
||||||
|
use Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests the Islandora Breadcrumbs Builder. |
||||||
|
* |
||||||
|
* @group islandora_breadcrumbs |
||||||
|
*/ |
||||||
|
class BreadcrumbsTest extends IslandoraFunctionalTestBase { |
||||||
|
|
||||||
|
use AssertBreadcrumbTrait; |
||||||
|
|
||||||
|
/** |
||||||
|
* Modules to enable. |
||||||
|
* |
||||||
|
* @var array |
||||||
|
*/ |
||||||
|
public static $modules = [ |
||||||
|
'islandora_breadcrumbs', |
||||||
|
]; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* A node. |
||||||
|
* |
||||||
|
* @var \Drupal\node\NodeInterface |
||||||
|
*/ |
||||||
|
protected $nodeA; |
||||||
|
|
||||||
|
/** |
||||||
|
* Another node. |
||||||
|
* |
||||||
|
* @var \Drupal\node\NodeInterface |
||||||
|
*/ |
||||||
|
protected $nodeB; |
||||||
|
|
||||||
|
/** |
||||||
|
* Yet another node. |
||||||
|
* |
||||||
|
* @var \Drupal\node\NodeInterface |
||||||
|
*/ |
||||||
|
protected $nodeC; |
||||||
|
|
||||||
|
/** |
||||||
|
* Another one. |
||||||
|
* |
||||||
|
* @var \Drupal\node\NodeInterface |
||||||
|
*/ |
||||||
|
protected $nodeD; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function setUp() { |
||||||
|
parent::setUp(); |
||||||
|
|
||||||
|
// Create some nodes. |
||||||
|
$this->nodeA = $this->container->get('entity_type.manager')->getStorage('node')->create([ |
||||||
|
'type' => $this->testType->id(), |
||||||
|
'title' => 'Node A', |
||||||
|
]); |
||||||
|
$this->nodeA->save(); |
||||||
|
|
||||||
|
$this->nodeB = $this->container->get('entity_type.manager')->getStorage('node')->create([ |
||||||
|
'type' => $this->testType->id(), |
||||||
|
'title' => 'Node B', |
||||||
|
]); |
||||||
|
$this->nodeB->set('field_member_of', [$this->nodeA->id()]); |
||||||
|
$this->nodeB->save(); |
||||||
|
|
||||||
|
$this->nodeC = $this->container->get('entity_type.manager')->getStorage('node')->create([ |
||||||
|
'type' => $this->testType->id(), |
||||||
|
'title' => 'Node C', |
||||||
|
]); |
||||||
|
$this->nodeC->set('field_member_of', [$this->nodeB->id()]); |
||||||
|
$this->nodeC->save(); |
||||||
|
|
||||||
|
$this->nodeD = $this->container->get('entity_type.manager')->getStorage('node')->create([ |
||||||
|
'type' => $this->testType->id(), |
||||||
|
'title' => 'Node D', |
||||||
|
]); |
||||||
|
$this->nodeD->set('field_member_of', [$this->nodeC->id()]); |
||||||
|
$this->nodeD->save(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora_breadcrumbs\IslandoraBreadcrumbBuilder::applies |
||||||
|
*/ |
||||||
|
public function testDefaults() { |
||||||
|
$breadcrumbs = [ |
||||||
|
$this->nodeC->toUrl()->toString() => $this->nodeC->label(), |
||||||
|
$this->nodeB->toUrl()->toString() => $this->nodeB->label(), |
||||||
|
$this->nodeA->toUrl()->toString() => $this->nodeA->label(), |
||||||
|
]; |
||||||
|
$this->assertBreadcrumb($this->nodeD->toUrl()->toString(), $breadcrumbs); |
||||||
|
|
||||||
|
// Create a reference loop. |
||||||
|
$this->nodeA->set('field_member_of', [$this->nodeD->id()]); |
||||||
|
$this->nodeA->save(); |
||||||
|
|
||||||
|
// We should still escape it and have the same trail as before. |
||||||
|
$this->assertBreadcrumb($this->nodeD->toUrl()->toString(), $breadcrumbs); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -1,35 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
namespace Drupal\islandora\Plugin\Action; |
|
||||||
|
|
||||||
use Drupal\Core\Action\ActionBase; |
|
||||||
use Drupal\Core\Session\AccountInterface; |
|
||||||
|
|
||||||
/** |
|
||||||
* Deletes a media. |
|
||||||
* |
|
||||||
* @Action( |
|
||||||
* id = "delete_media", |
|
||||||
* label = @Translation("Delete media"), |
|
||||||
* type = "media" |
|
||||||
* ) |
|
||||||
*/ |
|
||||||
class DeleteMedia extends ActionBase { |
|
||||||
|
|
||||||
/** |
|
||||||
* {@inheritdoc} |
|
||||||
*/ |
|
||||||
public function execute($entity = NULL) { |
|
||||||
if ($entity) { |
|
||||||
$entity->delete(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* {@inheritdoc} |
|
||||||
*/ |
|
||||||
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { |
|
||||||
return $object->access('delete', $account, $return_as_object); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -0,0 +1,174 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora\Plugin\Condition; |
||||||
|
|
||||||
|
use Drupal\Core\Condition\ConditionPluginBase; |
||||||
|
use Drupal\Core\Entity\EntityTypeManager; |
||||||
|
use Drupal\Core\Form\FormStateInterface; |
||||||
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
||||||
|
use Drupal\islandora\IslandoraUtils; |
||||||
|
use Drupal\islandora\MediaSource\MediaSourceService; |
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Provides a condition based on a node's media's MimeType. |
||||||
|
* |
||||||
|
* Note that this condition applies when the parent node is viewed. |
||||||
|
* It is not fired during ingest (i.e., it doesn't apply to |
||||||
|
* derivative generation). |
||||||
|
* |
||||||
|
* @Condition( |
||||||
|
* id = "media_has_mimetype", |
||||||
|
* label = @Translation("Media has Mime type"), |
||||||
|
* context_definitions = { |
||||||
|
* "node" = @ContextDefinition("entity:node", label = @Translation("node")) |
||||||
|
* } |
||||||
|
* ) |
||||||
|
*/ |
||||||
|
class MediaHasMimetype extends ConditionPluginBase implements ContainerFactoryPluginInterface { |
||||||
|
|
||||||
|
/** |
||||||
|
* Islandora utils. |
||||||
|
* |
||||||
|
* @var \Drupal\islandora\IslandoraUtils |
||||||
|
*/ |
||||||
|
protected $utils; |
||||||
|
|
||||||
|
/** |
||||||
|
* Term storage. |
||||||
|
* |
||||||
|
* @var \Drupal\Core\Entity\EntityTypeManager |
||||||
|
*/ |
||||||
|
protected $entityTypeManager; |
||||||
|
|
||||||
|
/** |
||||||
|
* A MediaSourceService. |
||||||
|
* |
||||||
|
* @var \Drupal\islandora\MediaSource\MediaSourceService |
||||||
|
*/ |
||||||
|
private $mediaSource; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructor. |
||||||
|
* |
||||||
|
* @param array $configuration |
||||||
|
* The plugin configuration, i.e. an array with configuration values keyed |
||||||
|
* by configuration option name. The special key 'context' may be used to |
||||||
|
* initialize the defined contexts by setting it to an array of context |
||||||
|
* values keyed by context names. |
||||||
|
* @param string $plugin_id |
||||||
|
* The plugin_id for the plugin instance. |
||||||
|
* @param mixed $plugin_definition |
||||||
|
* The plugin implementation definition. |
||||||
|
* @param \Drupal\islandora\IslandoraUtils $utils |
||||||
|
* Islandora utility functions. |
||||||
|
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager |
||||||
|
* Entity type manager. |
||||||
|
* @param \Drupal\islandora\MediaSource\MediaSourceService $media_source |
||||||
|
* Media source service. |
||||||
|
*/ |
||||||
|
public function __construct( |
||||||
|
array $configuration, |
||||||
|
$plugin_id, |
||||||
|
$plugin_definition, |
||||||
|
IslandoraUtils $utils, |
||||||
|
EntityTypeManager $entity_type_manager, |
||||||
|
MediaSourceService $media_source |
||||||
|
) { |
||||||
|
parent::__construct($configuration, $plugin_id, $plugin_definition, $utils); |
||||||
|
$this->utils = $utils; |
||||||
|
$this->entityTypeManager = $entity_type_manager; |
||||||
|
$this->mediaSource = $media_source; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||||||
|
return new static( |
||||||
|
$configuration, |
||||||
|
$plugin_id, |
||||||
|
$plugin_definition, |
||||||
|
$container->get('islandora.utils'), |
||||||
|
$container->get('entity_type.manager'), |
||||||
|
$container->get('islandora.media_source_service') |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
||||||
|
$form['mimetypes'] = [ |
||||||
|
'#type' => 'textfield', |
||||||
|
'#title' => t('Mime types'), |
||||||
|
'#default_value' => $this->configuration['mimetypes'], |
||||||
|
'#required' => TRUE, |
||||||
|
'#maxlength' => 256, |
||||||
|
'#description' => t('Comma-delimited list of Mime types (e.g. image/jpeg, video/mp4, etc...) that trigger the condition.'), |
||||||
|
]; |
||||||
|
return parent::buildConfigurationForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
||||||
|
$this->configuration['mimetypes'] = $form_state->getValue('mimetypes'); |
||||||
|
parent::submitConfigurationForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function summary() { |
||||||
|
$mimetypes = $this->configuration['mimetypes']; |
||||||
|
return $this->t( |
||||||
|
'The media has one of the Mime types @mimetypes', |
||||||
|
[ |
||||||
|
'@mimetypes' => $mimetypes, |
||||||
|
] |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function evaluate() { |
||||||
|
if (empty($this->configuration['mimetypes']) && !$this->isNegated()) { |
||||||
|
return TRUE; |
||||||
|
} |
||||||
|
|
||||||
|
$node = \Drupal::routeMatch()->getParameter('node'); |
||||||
|
|
||||||
|
if (is_null($node) || is_string($node)) { |
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
|
||||||
|
$media = $this->utils->getMedia($node); |
||||||
|
|
||||||
|
if (count($media) > 0) { |
||||||
|
$mimetypes = explode(',', str_replace(' ', '', $this->configuration['mimetypes'])); |
||||||
|
foreach ($media as $medium) { |
||||||
|
$file = $this->mediaSource->getSourceFile($medium); |
||||||
|
if (in_array($file->getMimeType(), $mimetypes)) { |
||||||
|
return $this->isNegated() ? FALSE : TRUE; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
else { |
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function defaultConfiguration() { |
||||||
|
return array_merge( |
||||||
|
['mimetypes' => ''], |
||||||
|
parent::defaultConfiguration() |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,187 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora\Plugin\Condition; |
||||||
|
|
||||||
|
use Drupal\Core\Condition\ConditionPluginBase; |
||||||
|
use Drupal\Core\Entity\EntityInterface; |
||||||
|
use Drupal\Core\Entity\EntityTypeManager; |
||||||
|
use Drupal\Core\Form\FormStateInterface; |
||||||
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
||||||
|
use Drupal\islandora\IslandoraUtils; |
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Provides a Islandora 7.x namespace condition for nodes. |
||||||
|
* |
||||||
|
* @Condition( |
||||||
|
* id = "node_had_namespace", |
||||||
|
* label = @Translation("Node had 7.x namespace"), |
||||||
|
* context = { |
||||||
|
* "node" = @ContextDefinition("entity:node", required = TRUE , label = @Translation("node")) |
||||||
|
* } |
||||||
|
* ) |
||||||
|
*/ |
||||||
|
class NodeHadNamespace extends ConditionPluginBase implements ContainerFactoryPluginInterface { |
||||||
|
|
||||||
|
/** |
||||||
|
* Islandora utils. |
||||||
|
* |
||||||
|
* @var \Drupal\islandora\IslandoraUtils |
||||||
|
*/ |
||||||
|
protected $utils; |
||||||
|
|
||||||
|
/** |
||||||
|
* Term storage. |
||||||
|
* |
||||||
|
* @var \Drupal\Core\Entity\EntityTypeManager |
||||||
|
*/ |
||||||
|
protected $entityTypeManager; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructor. |
||||||
|
* |
||||||
|
* @param array $configuration |
||||||
|
* The plugin configuration, i.e. an array with configuration values keyed |
||||||
|
* by configuration option name. The special key 'context' may be used to |
||||||
|
* initialize the defined contexts by setting it to an array of context |
||||||
|
* values keyed by context names. |
||||||
|
* @param string $plugin_id |
||||||
|
* The plugin_id for the plugin instance. |
||||||
|
* @param mixed $plugin_definition |
||||||
|
* The plugin implementation definition. |
||||||
|
* @param \Drupal\islandora\IslandoraUtils $utils |
||||||
|
* Islandora utils. |
||||||
|
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager |
||||||
|
* Entity type manager. |
||||||
|
*/ |
||||||
|
public function __construct( |
||||||
|
array $configuration, |
||||||
|
$plugin_id, |
||||||
|
$plugin_definition, |
||||||
|
IslandoraUtils $utils, |
||||||
|
EntityTypeManager $entity_type_manager |
||||||
|
) { |
||||||
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
||||||
|
$this->utils = $utils; |
||||||
|
$this->entityTypeManager = $entity_type_manager; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||||||
|
return new static( |
||||||
|
$configuration, |
||||||
|
$plugin_id, |
||||||
|
$plugin_definition, |
||||||
|
$container->get('islandora.utils'), |
||||||
|
$container->get('entity_type.manager') |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
||||||
|
$form['namespace'] = [ |
||||||
|
'#type' => 'textfield', |
||||||
|
'#title' => $this->t('Islandora 7.x Namespaces'), |
||||||
|
'#description' => $this->t('Comma-delimited list of 7.x PID namespaces, including the trailing colon (e.g., "islandora:,ir:").'), |
||||||
|
'#default_value' => $this->configuration['namespace'], |
||||||
|
'#maxlength' => 256, |
||||||
|
]; |
||||||
|
$field_map = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('string'); |
||||||
|
$node_fields = array_keys($field_map['node']); |
||||||
|
$options = array_combine($node_fields, $node_fields); |
||||||
|
$form['pid_field'] = [ |
||||||
|
'#type' => 'select', |
||||||
|
'#title' => t('Field that contains the PID'), |
||||||
|
'#options' => $options, |
||||||
|
'#default_value' => $this->configuration['pid_field'], |
||||||
|
'#required' => TRUE, |
||||||
|
'#description' => t("Machine name of the field that contains the PID."), |
||||||
|
]; |
||||||
|
|
||||||
|
return parent::buildConfigurationForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
||||||
|
$this->configuration['namespace'] = NULL; |
||||||
|
$namespace = $form_state->getValue('namespace'); |
||||||
|
if (!empty($namespace)) { |
||||||
|
if ($namespace) { |
||||||
|
$this->configuration['namespace'] = $namespace; |
||||||
|
} |
||||||
|
} |
||||||
|
$this->configuration['pid_field'] = $form_state->getValue('pid_field'); |
||||||
|
parent::submitConfigurationForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function evaluate() { |
||||||
|
if (empty($this->configuration['namespace']) && !$this->isNegated()) { |
||||||
|
return TRUE; |
||||||
|
} |
||||||
|
|
||||||
|
$node = $this->getContextValue('node'); |
||||||
|
if (!$node) { |
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
return $this->evaluateEntity($node); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Evaluates if the value of field_pid with a registered 7.x namespace. |
||||||
|
* |
||||||
|
* @param \Drupal\Core\Entity\EntityInterface $entity |
||||||
|
* The entity to evalute. |
||||||
|
* |
||||||
|
* @return bool |
||||||
|
* TRUE if entity has the specified namespace, otherwise FALSE. |
||||||
|
*/ |
||||||
|
protected function evaluateEntity(EntityInterface $entity) { |
||||||
|
$pid_field = $this->configuration['pid_field']; |
||||||
|
if ($entity->hasField($pid_field)) { |
||||||
|
$pid_value = $entity->get($pid_field)->getValue(); |
||||||
|
$pid = $pid_value[0]['value']; |
||||||
|
$namespace = strtok($pid, ':') . ':'; |
||||||
|
$registered_namespaces = explode(',', $this->configuration['namespace']); |
||||||
|
foreach ($registered_namespaces as &$registered_namespace) { |
||||||
|
$registered_namespace = trim($registered_namespace); |
||||||
|
if (in_array($namespace, $registered_namespaces)) { |
||||||
|
return $this->isNegated() ? FALSE : TRUE; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return $this->isNegated() ? TRUE : FALSE; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function summary() { |
||||||
|
if (!empty($this->configuration['negate'])) { |
||||||
|
return $this->t('The node does not have a value in its PID field with the namespace @namespace.', ['@namespace' => $this->configuration['namespace']]); |
||||||
|
} |
||||||
|
else { |
||||||
|
return $this->t('The node has a value in its PID field with the namespace @namespace.', ['@namespace' => $this->configuration['namespace']]); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function defaultConfiguration() { |
||||||
|
return array_merge( |
||||||
|
['namespace' => '', 'pid_field' => 'field_pid'], |
||||||
|
parent::defaultConfiguration() |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,168 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora\Plugin\Condition; |
||||||
|
|
||||||
|
use Drupal\Core\Condition\ConditionPluginBase; |
||||||
|
use Drupal\Core\Entity\EntityInterface; |
||||||
|
use Drupal\Core\Entity\EntityTypeManager; |
||||||
|
use Drupal\Core\Form\FormStateInterface; |
||||||
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Provides a condition to detect node's parent. |
||||||
|
* |
||||||
|
* @Condition( |
||||||
|
* id = "node_has_parent", |
||||||
|
* label = @Translation("Node has parent"), |
||||||
|
* context = { |
||||||
|
* "node" = @ContextDefinition("entity:node", required = TRUE , label = @Translation("node")) |
||||||
|
* } |
||||||
|
* ) |
||||||
|
*/ |
||||||
|
class NodeHasParent extends ConditionPluginBase implements ContainerFactoryPluginInterface { |
||||||
|
|
||||||
|
/** |
||||||
|
* Node storage. |
||||||
|
* |
||||||
|
* @var \Drupal\Core\Entity\EntityTypeManager |
||||||
|
*/ |
||||||
|
protected $entityTypeManager; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructor. |
||||||
|
* |
||||||
|
* @param array $configuration |
||||||
|
* The plugin configuration, i.e. an array with configuration values keyed |
||||||
|
* by configuration option name. The special key 'context' may be used to |
||||||
|
* initialize the defined contexts by setting it to an array of context |
||||||
|
* values keyed by context names. |
||||||
|
* @param string $plugin_id |
||||||
|
* The plugin_id for the plugin instance. |
||||||
|
* @param mixed $plugin_definition |
||||||
|
* The plugin implementation definition. |
||||||
|
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager |
||||||
|
* Entity type manager. |
||||||
|
*/ |
||||||
|
public function __construct( |
||||||
|
array $configuration, |
||||||
|
$plugin_id, |
||||||
|
$plugin_definition, |
||||||
|
EntityTypeManager $entity_type_manager |
||||||
|
) { |
||||||
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
||||||
|
$this->entityTypeManager = $entity_type_manager; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function defaultConfiguration() { |
||||||
|
return parent::defaultConfiguration() + [ |
||||||
|
'parent_reference_field' => 'field_member_of', |
||||||
|
'parent_nid' => '', |
||||||
|
]; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||||||
|
return new static( |
||||||
|
$configuration, |
||||||
|
$plugin_id, |
||||||
|
$plugin_definition, |
||||||
|
$container->get('entity_type.manager') |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
||||||
|
$form['parent_nid'] = [ |
||||||
|
'#type' => 'entity_autocomplete', |
||||||
|
'#title' => t('Parent node'), |
||||||
|
'#default_value' => $this->entityTypeManager->getStorage('node')->load($this->configuration['parent_nid']), |
||||||
|
'#required' => TRUE, |
||||||
|
'#description' => t("Can be a collection node or a compound object."), |
||||||
|
'#target_type' => 'node', |
||||||
|
]; |
||||||
|
$field_map = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('entity_reference'); |
||||||
|
$node_fields = array_keys($field_map['node']); |
||||||
|
$options = array_combine($node_fields, $node_fields); |
||||||
|
$form['parent_reference_field'] = [ |
||||||
|
'#type' => 'select', |
||||||
|
'#title' => t('Field that contains reference to parents'), |
||||||
|
'#options' => $options, |
||||||
|
'#default_value' => $this->configuration['parent_reference_field'], |
||||||
|
'#required' => TRUE, |
||||||
|
'#description' => t("Machine name of field that contains references to parent node."), |
||||||
|
]; |
||||||
|
|
||||||
|
return parent::buildConfigurationForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
||||||
|
$this->configuration['parent_nid'] = $form_state->getValue('parent_nid'); |
||||||
|
$this->configuration['parent_reference_field'] = $form_state->getValue('parent_reference_field'); |
||||||
|
parent::submitConfigurationForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function evaluate() { |
||||||
|
if (empty($this->configuration['parent_nid']) && !$this->isNegated()) { |
||||||
|
return TRUE; |
||||||
|
} |
||||||
|
|
||||||
|
$node = $this->getContextValue('node'); |
||||||
|
if (!$node) { |
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
return $this->evaluateEntity($node); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Evaluates if an entity has the specified node as its parent. |
||||||
|
* |
||||||
|
* @param \Drupal\Core\Entity\EntityInterface $entity |
||||||
|
* The entity to evalute. |
||||||
|
* |
||||||
|
* @return bool |
||||||
|
* TRUE if entity references the specified parent. |
||||||
|
*/ |
||||||
|
protected function evaluateEntity(EntityInterface $entity) { |
||||||
|
foreach ($entity->referencedEntities() as $referenced_entity) { |
||||||
|
if ($entity->getEntityTypeID() == 'node' && $referenced_entity->getEntityTypeId() == 'node') { |
||||||
|
$parent_reference_field = $this->configuration['parent_reference_field']; |
||||||
|
$field = $entity->get($parent_reference_field); |
||||||
|
if (!$field->isEmpty()) { |
||||||
|
$nids = $field->getValue(); |
||||||
|
foreach ($nids as $nid) { |
||||||
|
if ($nid['target_id'] == $this->configuration['parent_nid']) { |
||||||
|
return $this->isNegated() ? FALSE : TRUE; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function summary() { |
||||||
|
if (!empty($this->configuration['negate'])) { |
||||||
|
return $this->t('The node does not have node @nid as its parent.', ['@nid' => $this->configuration['parent_nid']]); |
||||||
|
} |
||||||
|
else { |
||||||
|
return $this->t('The node has node @nid as its parent.', ['@nid' => $this->configuration['parent_nid']]); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,94 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora\Plugin\Condition; |
||||||
|
|
||||||
|
use Drupal\Core\Condition\ConditionPluginBase; |
||||||
|
use Drupal\Core\Entity\EntityTypeManager; |
||||||
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Provides an 'Is Published' condition for nodes. |
||||||
|
* |
||||||
|
* @Condition( |
||||||
|
* id = "node_is_published", |
||||||
|
* label = @Translation("Node is published"), |
||||||
|
* context = { |
||||||
|
* "node" = @ContextDefinition("entity:node", required = TRUE , label = @Translation("node")) |
||||||
|
* } |
||||||
|
* ) |
||||||
|
*/ |
||||||
|
class NodeIsPublished extends ConditionPluginBase implements ContainerFactoryPluginInterface { |
||||||
|
|
||||||
|
/** |
||||||
|
* Term storage. |
||||||
|
* |
||||||
|
* @var \Drupal\Core\Entity\EntityTypeManager |
||||||
|
*/ |
||||||
|
protected $entityTypeManager; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructor. |
||||||
|
* |
||||||
|
* @param array $configuration |
||||||
|
* The plugin configuration, i.e. an array with configuration values keyed |
||||||
|
* by configuration option name. The special key 'context' may be used to |
||||||
|
* initialize the defined contexts by setting it to an array of context |
||||||
|
* values keyed by context names. |
||||||
|
* @param string $plugin_id |
||||||
|
* The plugin_id for the plugin instance. |
||||||
|
* @param mixed $plugin_definition |
||||||
|
* The plugin implementation definition. |
||||||
|
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager |
||||||
|
* Entity type manager. |
||||||
|
*/ |
||||||
|
public function __construct( |
||||||
|
array $configuration, |
||||||
|
$plugin_id, |
||||||
|
$plugin_definition, |
||||||
|
EntityTypeManager $entity_type_manager |
||||||
|
) { |
||||||
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
||||||
|
$this->entityTypeManager = $entity_type_manager; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||||||
|
return new static( |
||||||
|
$configuration, |
||||||
|
$plugin_id, |
||||||
|
$plugin_definition, |
||||||
|
$container->get('entity_type.manager') |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function evaluate() { |
||||||
|
$node = $this->getContextValue('node'); |
||||||
|
if (!$node && !$this->isNegated()) { |
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
if ($node->isPublished() && !$this->isNegated()) { |
||||||
|
return TRUE; |
||||||
|
} |
||||||
|
|
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function summary() { |
||||||
|
if (!empty($this->configuration['negate'])) { |
||||||
|
return $this->t('The node is not published.'); |
||||||
|
} |
||||||
|
else { |
||||||
|
return $this->t('The node is published.'); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue