-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from discoverygarden/DDST-166
DDST 166: Make handle suffix configurable
- Loading branch information
Showing
9 changed files
with
291 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
modules/dgi_actions_handle/modules/dgi_actions_handle_constraints/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# DGI Actions Handle Constraints Module | ||
|
||
## Introduction | ||
|
||
This module is part of the DGI Actions Handle package. It provides functionality to add unique constraints to certain fields of an entity and handles the preservation of these fields during entity save operations. | ||
|
||
## Notes | ||
|
||
* This module should only be installed when a field is being used as a suffix field. | ||
* The developer or site administrator should note that this module would make the suffix field always required and unique. | ||
|
||
## Requirements | ||
|
||
This module requires the following modules/libraries: | ||
|
||
* [DGI Actions Handle](https://github.com/discoverygarden/dgi_actions_handle) | ||
|
||
## Installation | ||
|
||
Install as usual, see | ||
[this](https://www.drupal.org/docs/extending-drupal/installing-modules) for | ||
further information. | ||
|
||
## Features | ||
|
||
1. **Unique Constraints**: The module can add a unique constraint to specified fields of an entity. | ||
|
||
2. **Entity Base Field Alteration**: The module implements the `hook_entity_base_field_info_alter` hook to add constraints to the configured fields. | ||
|
||
3. **Entity Bundle Field Alteration**: The module implements the `hook_entity_bundle_field_info_alter` hook to add constraints to the configured fields. | ||
|
||
4. **Entity Pre-save**: The module implements the `hook_entity_presave` hook to revert the value of the suffix field if it is changed. This is needed for spreadsheet ingest. | ||
|
||
5. **Form Alteration**: The module implements the `hook_form_alter` hook to disable the suffix/identifier fields that are not allowed to be changed. | ||
|
||
## Usage | ||
|
||
This module is used as part of the DGI Actions Handle package. | ||
It is used to ensure that the fields that are used as suffixes for the Handle are unique and required. The module also ensures that the suffix field is not changed during entity save operations. | ||
Once the configuration is set up, the module will handle the rest. | ||
|
||
## Configuration | ||
|
||
The module uses the `dgi_actions_handle_constraints.settings` configuration, which should be set up with the appropriate constraint settings. | ||
For each field that is used as a suffix or identifier, a new value should be added to the constraint_settings array. A default configuration file which | ||
uses the field_pid as suffix and field_handle as identifier is provided with the module. This can be updated with the appropriate field names. | ||
|
||
## Troubleshooting/Issues | ||
|
||
Having problems or solved a problem? Contact | ||
[discoverygarden](http://support.discoverygarden.ca). | ||
|
||
## Maintainers/Sponsors | ||
|
||
Current maintainers: | ||
|
||
* [discoverygarden](http://www.discoverygarden.ca) | ||
|
||
## Development | ||
|
||
If you would like to contribute to this module, please check out the helpful | ||
[Documentation](https://github.com/Islandora/islandora/wiki#wiki-documentation-for-developers), | ||
[Developers](http://islandora.ca/developers) section on Islandora.ca and | ||
contact [discoverygarden](http://support.discoverygarden.ca). | ||
|
||
## License | ||
|
||
[GPLv3](http://www.gnu.org/licenses/gpl-3.0.txt) |
9 changes: 9 additions & 0 deletions
9
...dgi_actions_handle_constraints/config/install/dgi_actions_handle_constraints.settings.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
constraint_settings: | ||
- entity_type: node | ||
entity_bundle: islandora_object | ||
field_name: field_pid | ||
field_usage: suffix | ||
- entity_type: node | ||
entity_bundle: islandora_object | ||
field_name: field_handle | ||
field_usage: identifier |
27 changes: 27 additions & 0 deletions
27
...es/dgi_actions_handle_constraints/config/schema/dgi_actions_handle_constraints.schema.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
dgi_action_handle_constraints.settings: | ||
type: config_object | ||
label: 'DGI Actions constraint settings' | ||
mapping: | ||
constraint_settings: | ||
type: mapping | ||
label: 'Constraint data' | ||
mapping: | ||
entity_type: | ||
type: string | ||
label: 'Entity type' | ||
description: 'The entity type to add the constraint to.' | ||
entity_bundle: | ||
type: string | ||
label: 'Entity bundle' | ||
description: 'The entity bundle to add the constraint to.' | ||
field_name: | ||
type: string | ||
label: 'Field name' | ||
description: 'The field name to add the constraint to.' | ||
field_usage: | ||
type: string | ||
label: 'Field usage' | ||
description: 'Either identifier or suffix.' | ||
constraints: | ||
Regex: '/^(identifier|suffix)$/' |
7 changes: 7 additions & 0 deletions
7
...ons_handle/modules/dgi_actions_handle_constraints/dgi_actions_handle_constraints.info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: 'DGI Actions Handle Constraints' | ||
description: "Add constraints to handle fields." | ||
type: module | ||
package: DGI Actions | ||
core_version_requirement: ^10 | ||
dependencies: | ||
- dgi_actions_handle:dgi_actions_handle |
141 changes: 141 additions & 0 deletions
141
...tions_handle/modules/dgi_actions_handle_constraints/dgi_actions_handle_constraints.module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains the dgi_actions_handle_constraints module. | ||
*/ | ||
|
||
use Drupal\Core\Entity\ContentEntityInterface; | ||
use Drupal\Core\Entity\ContentEntityTypeInterface; | ||
use Drupal\Core\Entity\EntityInterface; | ||
use Drupal\Core\Entity\EntityTypeInterface; | ||
use Drupal\Core\Form\FormStateInterface; | ||
|
||
/** | ||
* Implements hook_entity_base_field_info_alter(). | ||
* | ||
* Adds the UniqueField, Required constraint to the suffix and identifer fields. | ||
*/ | ||
function _dgi_actions_handle_constraints_suffix_validation_add_constraint(array &$fields, EntityTypeInterface $entity_type): void { | ||
try { | ||
$config = \Drupal::config('dgi_actions_handle_constraints.settings'); | ||
$constraint_settings = $config->get('constraint_settings') ?? []; | ||
// Constraint settings is an array of values | ||
// for each field that should be modified. | ||
foreach ($constraint_settings as $constraintSetting) { | ||
if (!isset($fields[$constraintSetting['field_name']]) || $constraintSetting['entity_type'] !== $entity_type->id()) { | ||
continue; | ||
} | ||
// Both identifier and suffix fields should be unique. | ||
$fields[$constraintSetting['field_name']]->addConstraint('UniqueField'); | ||
|
||
// If the field is a suffix field, set it as required. | ||
if ($constraintSetting['field_usage'] === 'suffix') { | ||
$fields[$constraintSetting['field_name']]->setRequired(TRUE); | ||
} | ||
} | ||
} | ||
catch (\Exception $e) { | ||
Drupal::logger('dgi_actions_handle_constraints')->error($e->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Implements hook_entity_base_field_info_alter(). | ||
*/ | ||
function dgi_actions_handle_constraints_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type): void { | ||
if ($entity_type instanceof ContentEntityTypeInterface) { | ||
_dgi_actions_handle_constraints_suffix_validation_add_constraint($fields, $entity_type); | ||
} | ||
} | ||
|
||
/** | ||
* Implements hook_entity_bundle_field_info_alter(). | ||
*/ | ||
function dgi_actions_handle_constraints_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type): void { | ||
if ($entity_type instanceof ContentEntityTypeInterface) { | ||
_dgi_actions_handle_constraints_suffix_validation_add_constraint($fields, $entity_type); | ||
} | ||
} | ||
|
||
/** | ||
* Implements hook_entity_presave(). | ||
* | ||
* Reverts the value of the suffix field if it is changed. | ||
* This is needed for spreadsheet ingest. | ||
*/ | ||
function dgi_actions_handle_constraints_entity_presave(EntityInterface $entity): void { | ||
try { | ||
$config = \Drupal::config('dgi_actions_handle_constraints.settings'); | ||
$constraint_settings = $config->get('constraint_settings'); | ||
// Constraint settings is an array of values | ||
// for each field that should be modified. | ||
foreach ($constraint_settings as $constraintSetting) { | ||
// Only revert the suffix field if it is changed. | ||
if ($constraintSetting['field_usage'] !== 'suffix' | ||
|| $entity->getEntityTypeId() !== $constraintSetting['entity_type'] | ||
|| $entity->isNew() || !$entity->hasField($constraintSetting['field_name'])) { | ||
continue; | ||
} | ||
$original_entity = Drupal::entityTypeManager() | ||
->getStorage($entity->getEntityTypeId()) | ||
->loadUnchanged($entity->id()); | ||
if (!$original_entity) { | ||
return; | ||
} | ||
if ($entity->{$constraintSetting['field_name']}->value !== $original_entity->{$constraintSetting['field_name']}->value) { | ||
$entity->{$constraintSetting['field_name']}->value = $original_entity->{$constraintSetting['field_name']}->value; | ||
Drupal::messenger()->addWarning('The suffix field cannot be changed.'); | ||
} | ||
} | ||
} | ||
catch (\Exception $e) { | ||
Drupal::logger('dgi_actions_handle_constraints')->error($e->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Implements hook_form_alter(). | ||
* | ||
* Disables the suffix/identifier fields that are not allowed to be changed. | ||
*/ | ||
function dgi_actions_handle_constraints_form_alter(array &$form, FormStateInterface $form_state, $form_id): void { | ||
try { | ||
$config = \Drupal::config('dgi_actions_handle_constraints.settings'); | ||
$constraint_settings = $config->get('constraint_settings'); | ||
// Constraint settings is an array | ||
// of values for each field that should be modified. | ||
foreach ($constraint_settings as $constraintSetting) { | ||
// If the form id is not of the content | ||
// form or the content edit form, return. | ||
if (!($form_id === $constraintSetting['entity_type'] . '_' . $constraintSetting['entity_bundle'] . '_form' | ||
|| $form_id === $constraintSetting['entity_type'] . '_' . $constraintSetting['entity_bundle'] . '_edit_form')) { | ||
return; | ||
} | ||
$entity = $form_state->getFormObject()->getEntity(); | ||
if (!$entity instanceof ContentEntityInterface || !isset($form[$constraintSetting['field_name']])) { | ||
continue; | ||
} | ||
// For the suffix field, set the description and access. | ||
if ($constraintSetting['field_usage'] === 'suffix') { | ||
$form[$constraintSetting['field_name']]['widget'][0]['value']['#description'] = t('This field is used as the handle suffix. The suffix field, once set, cannot be changed.'); | ||
$form[$constraintSetting['field_name']]['#access'] = TRUE; | ||
|
||
// Disable the suffix field if it has a value. | ||
if ($entity->{$constraintSetting['field_name']}->value && !$entity->isNew()) { | ||
$form[$constraintSetting['field_name']]['#disabled'] = TRUE; | ||
} | ||
} | ||
|
||
// If the field is an identifier | ||
// field and the entity is not new, disable it. | ||
if ($constraintSetting['field_usage'] === 'identifier' && !$entity->isNew()) { | ||
$form[$constraintSetting['field_name']]['#disabled'] = TRUE; | ||
} | ||
|
||
} | ||
} | ||
catch (\Exception $e) { | ||
Drupal::logger('dgi_actions_handle_constraints')->error($e->getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters