12 changed files with 449 additions and 4 deletions
@ -0,0 +1,8 @@ |
|||||||
|
# Schema for the configuration files of the ScholarTokens module. |
||||||
|
scholartokens.settings: |
||||||
|
type: config_object |
||||||
|
label: 'ScholarTokens settings' |
||||||
|
mapping: |
||||||
|
fields: |
||||||
|
type: string |
||||||
|
label: 'Choose date field to be tokenized' |
||||||
@ -0,0 +1,6 @@ |
|||||||
|
scholartokens.scholar_tokens_settings: |
||||||
|
title: Scholar tokens settings |
||||||
|
description: set default field for token |
||||||
|
parent: system.admin_config_search |
||||||
|
route_name: scholartokens.scholar_tokens_settings |
||||||
|
weight: 10 |
||||||
@ -0,0 +1,7 @@ |
|||||||
|
scholartokens.scholar_tokens_settings: |
||||||
|
path: '/admin/search/scholar-tokens-settings' |
||||||
|
defaults: |
||||||
|
_title: 'Scholar tokens settings' |
||||||
|
_form: 'Drupal\scholartokens\Form\ScholarTokensSettingsForm' |
||||||
|
requirements: |
||||||
|
_permission: 'administer site configuration' |
||||||
@ -0,0 +1,24 @@ |
|||||||
|
# Scholar Tokens |
||||||
|
|
||||||
|
Tokens to build Google Scholar Metatags |
||||||
|
|
||||||
|
## Introduction |
||||||
|
|
||||||
|
Google Scholar Metatags has specific requirements for the formatting of their inputs. This module provides some tokens to make that work |
||||||
|
|
||||||
|
## Installation |
||||||
|
|
||||||
|
Install as |
||||||
|
[usual](https://www.drupal.org/docs/extending-drupal/installing-modules). |
||||||
|
|
||||||
|
## Usage |
||||||
|
|
||||||
|
After installing and enabling module, navigate to `admin/config/search/metatag` and edit Content:Repository Item. The new tokens can be added to any field. |
||||||
|
|
||||||
|
## Maintainers |
||||||
|
Current maintainers: |
||||||
|
|
||||||
|
* [Robertson Library](https://library.upei.ca/) |
||||||
|
|
||||||
|
## License |
||||||
|
[GPLv3](http://www.gnu.org/licenses/gpl-3.0.txt) |
||||||
@ -0,0 +1,8 @@ |
|||||||
|
# Schema for the configuration files of the ScholarTokens module. |
||||||
|
scholartokens.settings: |
||||||
|
type: config_object |
||||||
|
label: 'ScholarTokens settings' |
||||||
|
mapping: |
||||||
|
fields: |
||||||
|
type: string |
||||||
|
label: 'Choose date field to be tokenized' |
||||||
@ -0,0 +1,7 @@ |
|||||||
|
name: 'ScholarTokens' |
||||||
|
type: module |
||||||
|
description: 'supplies tokens for the Scholar metatags' |
||||||
|
package: Custom |
||||||
|
core_version_requirement: ^10 || ^11 |
||||||
|
dependencies: |
||||||
|
- token:token |
||||||
@ -0,0 +1,6 @@ |
|||||||
|
scholartokens.scholar_tokens_settings: |
||||||
|
title: Scholar tokens settings |
||||||
|
description: set default field for token |
||||||
|
parent: system.admin_config_search |
||||||
|
route_name: scholartokens.scholar_tokens_settings |
||||||
|
weight: 10 |
||||||
@ -0,0 +1,141 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
/** |
||||||
|
* @file |
||||||
|
* Primary module hooks for ScholarTokens module. |
||||||
|
*/ |
||||||
|
|
||||||
|
use Drupal\taxonomy\Entity\Term; |
||||||
|
use Drupal\Core\Render\BubbleableMetadata; |
||||||
|
|
||||||
|
/** |
||||||
|
* Implements hook_token_info(). |
||||||
|
*/ |
||||||
|
function scholartokens_token_info() { |
||||||
|
$type = [ |
||||||
|
'name' => t('Scholar Tokens'), |
||||||
|
'description' => t('Supplemental Tokens for Islandora Scholar objects.'), |
||||||
|
]; |
||||||
|
|
||||||
|
$node['scholar_author'] = [ |
||||||
|
'name' => t("Author"), |
||||||
|
'description' => t("The node's filtered Contributor linked agents"), |
||||||
|
]; |
||||||
|
|
||||||
|
$node['scholar_date_issued'] = [ |
||||||
|
'name' => t("Date Issued"), |
||||||
|
'description' => t("The node's Issued Date"), |
||||||
|
]; |
||||||
|
|
||||||
|
return [ |
||||||
|
'types' => ['scholartokens' => $type], |
||||||
|
'tokens' => ['scholartokens' => $node], |
||||||
|
]; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Implements hook_tokens(). |
||||||
|
*/ |
||||||
|
function scholartokens_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) { |
||||||
|
$replacements = []; |
||||||
|
if ($type == 'scholartokens' && !empty($data['node'])) { |
||||||
|
if (!is_array($tokens) || empty($tokens)) { |
||||||
|
\Drupal::logger('scholartokens') |
||||||
|
->alert('Tokens not correct format: @tokens', [ |
||||||
|
'@tokens' => print_r($tokens, 1), |
||||||
|
]); |
||||||
|
return; |
||||||
|
} |
||||||
|
foreach ($tokens as $name => $original) { |
||||||
|
switch ($name) { |
||||||
|
case 'scholar_author': |
||||||
|
$allowed = ['relators:aut', 'relators:cre']; |
||||||
|
$replacement = scholartokens_get_term_with_rel_type($data['node'], 'field_linked_agent', $allowed, TRUE); |
||||||
|
$replacements[$original] = $replacement; |
||||||
|
break; |
||||||
|
|
||||||
|
case 'scholar_date_issued': |
||||||
|
$node = $data['node']; |
||||||
|
$replacement = scholartokens_format_date($node); |
||||||
|
$replacements[$original] = $replacement; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return $replacements; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Date formatter. |
||||||
|
* |
||||||
|
* @param object $node |
||||||
|
* A core drupal node object. |
||||||
|
*/ |
||||||
|
function scholartokens_format_date($node) { |
||||||
|
$config = \Drupal::config('scholartokens.settings'); |
||||||
|
$field = $config->get('field'); |
||||||
|
$original = $node->get($field)->value; |
||||||
|
$date_parts = explode('-', $original); |
||||||
|
if (count($date_parts) == 1) { |
||||||
|
$replacement = $date_parts[0]; |
||||||
|
} |
||||||
|
if (count($date_parts) == 2) { |
||||||
|
$replacement = $date_parts[0]; |
||||||
|
} |
||||||
|
if (count($date_parts) == 3) { |
||||||
|
$replacement = implode('/', $date_parts); |
||||||
|
} |
||||||
|
|
||||||
|
return $replacement; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Helper function to load values for a taxonomy term with a relationship type. |
||||||
|
* |
||||||
|
* @param object $node |
||||||
|
* A core drupal node object. |
||||||
|
* @param string $field_name |
||||||
|
* The name of the node's field to check for the specific relationship. |
||||||
|
* @param array $relation_types |
||||||
|
* Optional values to check the rel_type of the taxonomy term against. When |
||||||
|
* not provided, any terms returned for the field will match. |
||||||
|
* @param bool $remove_comma |
||||||
|
* Will flip the string parts of a CSV. |
||||||
|
* |
||||||
|
* @return string |
||||||
|
* The tokenized value for the given data. |
||||||
|
*/ |
||||||
|
function scholartokens_get_term_with_rel_type($node, $field_name, array $relation_types = [], $remove_comma = FALSE) { |
||||||
|
$settings = \Drupal::config('metatag.settings'); |
||||||
|
$separator = $settings->get('separator'); |
||||||
|
$separator = ($separator) ? $separator : ','; |
||||||
|
$matches = []; |
||||||
|
$field = ($node->hasField($field_name) ? $node->get($field_name) : NULL); |
||||||
|
if (is_object($field)) { |
||||||
|
$tids = $field->getValue(); |
||||||
|
foreach ($tids as $tid) { |
||||||
|
// Inspect the taxonomy term. |
||||||
|
if ( |
||||||
|
is_array($tid) && |
||||||
|
array_key_exists('target_id', $tid) |
||||||
|
) { |
||||||
|
if ($relation_types && array_key_exists('rel_type', $tid)) { |
||||||
|
if (in_array($tid['rel_type'], $relation_types)) { |
||||||
|
$term = Term::load($tid['target_id']); |
||||||
|
if ($term) { |
||||||
|
$matches[] = ($separator == ',') ? controlled_access_terms_remove_comma($term->getName(), $remove_comma) : $term->getName(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
else { |
||||||
|
$term = Term::load($tid['target_id']); |
||||||
|
if ($term) { |
||||||
|
$matches[] = ($separator == ',') ? controlled_access_terms_remove_comma($term->getName(), $remove_comma) : $term->getName(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return implode($separator, $matches); |
||||||
|
} |
||||||
@ -0,0 +1,7 @@ |
|||||||
|
scholartokens.scholar_tokens_settings: |
||||||
|
path: '/admin/search/scholar-tokens-settings' |
||||||
|
defaults: |
||||||
|
_title: 'Scholar tokens settings' |
||||||
|
_form: 'Drupal\scholartokens\Form\ScholarTokensSettingsForm' |
||||||
|
requirements: |
||||||
|
_permission: 'administer site configuration' |
||||||
@ -0,0 +1,114 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
declare(strict_types=1); |
||||||
|
|
||||||
|
namespace Drupal\scholartokens\Form; |
||||||
|
|
||||||
|
use Drupal\Core\Entity\EntityFieldManagerInterface; |
||||||
|
use Drupal\Core\Form\ConfigFormBase; |
||||||
|
use Drupal\Core\Form\FormStateInterface; |
||||||
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Configure ScholarTokens settings for this site. |
||||||
|
*/ |
||||||
|
final class ScholarTokensSettingsForm extends ConfigFormBase { |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function getFormId(): string { |
||||||
|
return 'scholartokens_scholar_tokens_settings'; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
protected function getEditableConfigNames(): array { |
||||||
|
return ['scholartokens.settings']; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* The entity type manager service. |
||||||
|
* |
||||||
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||||||
|
*/ |
||||||
|
protected $entityTypeManager; |
||||||
|
|
||||||
|
/** |
||||||
|
* The entity field manager service. |
||||||
|
* |
||||||
|
* @var \Drupal\Core\Entity\EntityFieldManagerInterface |
||||||
|
*/ |
||||||
|
protected $entityFieldManager; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs a new CitationSelectSettingsForm. |
||||||
|
* |
||||||
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
||||||
|
* The entity type manager service. |
||||||
|
*/ |
||||||
|
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $entity_field_manager) { |
||||||
|
$this->entityTypeManager = $entityTypeManager; |
||||||
|
$this->entityFieldManager = $entity_field_manager; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public static function create(ContainerInterface $container) { |
||||||
|
return new static( |
||||||
|
$container->get('entity_type.manager'), |
||||||
|
$container->get('entity_field.manager') |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function buildForm(array $form, FormStateInterface $form_state): array { |
||||||
|
$fields = $this->entityFieldManager->getFieldDefinitions('node', 'islandora_object'); |
||||||
|
$date_fields = []; |
||||||
|
foreach ($fields as $field_name => $field_definition) { |
||||||
|
if ($field_definition->getType() === 'edtf') { |
||||||
|
$date_fields[$field_name] = $field_definition->getLabel(); |
||||||
|
} |
||||||
|
} |
||||||
|
$form['field'] = [ |
||||||
|
'#type' => 'select', |
||||||
|
'#title' => $this->t('Choose date field to be tokenized'), |
||||||
|
'#options' => $date_fields, |
||||||
|
'#default_value' => $this->config('scholartokens.settings')->get('field'), |
||||||
|
]; |
||||||
|
return parent::buildForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function validateForm(array &$form, FormStateInterface $form_state): void { |
||||||
|
// @todo Validate the form here. |
||||||
|
// Example: |
||||||
|
// @code |
||||||
|
// if ($form_state->getValue('example') === 'wrong') { |
||||||
|
// $form_state->setErrorByName( |
||||||
|
// 'message', |
||||||
|
// $this->t('The value is not correct.'), |
||||||
|
// ); |
||||||
|
// } |
||||||
|
// @endcode |
||||||
|
parent::validateForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function submitForm(array &$form, FormStateInterface $form_state): void { |
||||||
|
$this->config('scholartokens.settings') |
||||||
|
->set('field', $form_state->getValue('field')) |
||||||
|
->save(); |
||||||
|
parent::submitForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,114 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
declare(strict_types=1); |
||||||
|
|
||||||
|
namespace Drupal\scholartokens\Form; |
||||||
|
|
||||||
|
use Drupal\Core\Entity\EntityFieldManagerInterface; |
||||||
|
use Drupal\Core\Form\ConfigFormBase; |
||||||
|
use Drupal\Core\Form\FormStateInterface; |
||||||
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Configure ScholarTokens settings for this site. |
||||||
|
*/ |
||||||
|
final class ScholarTokensSettingsForm extends ConfigFormBase { |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function getFormId(): string { |
||||||
|
return 'scholartokens_scholar_tokens_settings'; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
protected function getEditableConfigNames(): array { |
||||||
|
return ['scholartokens.settings']; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* The entity type manager service. |
||||||
|
* |
||||||
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||||||
|
*/ |
||||||
|
protected $entityTypeManager; |
||||||
|
|
||||||
|
/** |
||||||
|
* The entity field manager service. |
||||||
|
* |
||||||
|
* @var \Drupal\Core\Entity\EntityFieldManagerInterface |
||||||
|
*/ |
||||||
|
protected $entityFieldManager; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs a new CitationSelectSettingsForm. |
||||||
|
* |
||||||
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
||||||
|
* The entity type manager service. |
||||||
|
*/ |
||||||
|
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $entity_field_manager) { |
||||||
|
$this->entityTypeManager = $entityTypeManager; |
||||||
|
$this->entityFieldManager = $entity_field_manager; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public static function create(ContainerInterface $container) { |
||||||
|
return new static( |
||||||
|
$container->get('entity_type.manager'), |
||||||
|
$container->get('entity_field.manager') |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function buildForm(array $form, FormStateInterface $form_state): array { |
||||||
|
$fields = $this->entityFieldManager->getFieldDefinitions('node', 'islandora_object'); |
||||||
|
$date_fields = []; |
||||||
|
foreach ($fields as $field_name => $field_definition) { |
||||||
|
if ($field_definition->getType() === 'edtf') { |
||||||
|
$date_fields[$field_name] = $field_definition->getLabel(); |
||||||
|
} |
||||||
|
} |
||||||
|
$form['field'] = [ |
||||||
|
'#type' => 'select', |
||||||
|
'#title' => $this->t('Choose date field to be tokenized'), |
||||||
|
'#options' => $date_fields, |
||||||
|
'#default_value' => $this->config('scholartokens.settings')->get('field'), |
||||||
|
]; |
||||||
|
return parent::buildForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function validateForm(array &$form, FormStateInterface $form_state): void { |
||||||
|
// @todo Validate the form here. |
||||||
|
// Example: |
||||||
|
// @code |
||||||
|
// if ($form_state->getValue('example') === 'wrong') { |
||||||
|
// $form_state->setErrorByName( |
||||||
|
// 'message', |
||||||
|
// $this->t('The value is not correct.'), |
||||||
|
// ); |
||||||
|
// } |
||||||
|
// @endcode |
||||||
|
parent::validateForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function submitForm(array &$form, FormStateInterface $form_state): void { |
||||||
|
$this->config('scholartokens.settings') |
||||||
|
->set('field', $form_state->getValue('field')) |
||||||
|
->save(); |
||||||
|
parent::submitForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue