You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.3 KiB
74 lines
2.3 KiB
<?php |
|
|
|
/** |
|
* @file |
|
* Custom hooks for Islandlives site. |
|
*/ |
|
|
|
use Drupal\pathauto\PathautoPatternInterface; |
|
use Drupal\views\ViewExecutable; |
|
|
|
/** |
|
* Implements hook_pathauto_pattern_alter(). |
|
*/ |
|
function islandlives_pathauto_pattern_alter(PathautoPatternInterface $pattern, array $context) { |
|
// Only act on the repository_items pattern. |
|
if ($pattern->id() == 'repository_items') { |
|
// If node has no PID... |
|
$node = $context['data']['node']; |
|
$pid = $node->get('field_pid')->value; |
|
if ($pid == NULL) { |
|
// Replace the PID with the NID in the alias. |
|
$new_pattern = preg_replace('/\[node:field_pid\]/', '[node:nid]', $pattern->getPattern()); |
|
$pattern->setPattern($new_pattern); |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* Implements hook_views_pre_render(). |
|
*/ |
|
function islandlives_views_pre_render(ViewExecutable $view) { |
|
$applicable_views = [ |
|
'content', |
|
'media', |
|
'manage_members', |
|
'media_of', |
|
'all_scholars', |
|
]; |
|
if (isset($view) && (in_array($view->storage->id(), $applicable_views))) { |
|
$view->element['#attached']['library'][] = 'islandlives/unpublished'; |
|
} |
|
} |
|
|
|
/** |
|
* Implements hook_entity_access(). |
|
*/ |
|
function islandlives_entity_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account) { |
|
// Only modify access for 'view' and 'download' operations, and only for anonymous users. |
|
if (!($operation === 'view' || $operation === 'download') || !$account->isAnonymous()) { |
|
return \Drupal\Core\Access\AccessResult::neutral(); |
|
} |
|
|
|
if ($entity instanceof \Drupal\media\MediaInterface) { |
|
if (!$entity->isPublished()) { |
|
return \Drupal\Core\Access\AccessResult::forbidden('Media is not published.'); |
|
} |
|
} |
|
elseif ($entity instanceof \Drupal\file\FileInterface) { |
|
// Get all media attached to the files. |
|
$media = \Drupal::database()->select(\Drupal\islandora_hierarchical_access\LUTGeneratorInterface::TABLE_NAME, 'lut') |
|
->fields('lut', ['mid']) |
|
->condition("lut.fid", $entity->id()) |
|
->execute() |
|
->fetchCol(); |
|
|
|
foreach ($media as $mid) { |
|
$media_entity = \Drupal::entityTypeManager()->getStorage('media')->load($mid); |
|
if ($media_entity && !$media_entity->isPublished()) { |
|
return \Drupal\Core\Access\AccessResult::forbidden('Media is not published.'); |
|
} |
|
} |
|
} |
|
} |
|
|
|
|