From 092c7047888b980a463cbfdf76decf760b50f168 Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Thu, 20 Jun 2024 09:10:25 -0300 Subject: [PATCH] Use a later hook to tweak the options. --- ...led_access_terms.field_type_categories.yml | 2 +- controlled_access_terms.libraries.yml | 2 +- controlled_access_terms.module | 22 +++++++++++++++++++ css/controlled_access_terms.icons-min.css | 1 + src/Plugin/Field/FieldType/TypedRelation.php | 2 +- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/controlled_access_terms.field_type_categories.yml b/controlled_access_terms.field_type_categories.yml index 9e3f8f6..cab2182 100644 --- a/controlled_access_terms.field_type_categories.yml +++ b/controlled_access_terms.field_type_categories.yml @@ -1,6 +1,6 @@ typed_relation: label: 'Typed Relation' description: 'A Reference field, with an associated type selected from a configurable list.' - weight: 10 + weight: 100 libraries: - controlled_access_terms/controlled_access_terms.icons diff --git a/controlled_access_terms.libraries.yml b/controlled_access_terms.libraries.yml index 329d531..50381bc 100644 --- a/controlled_access_terms.libraries.yml +++ b/controlled_access_terms.libraries.yml @@ -1,4 +1,4 @@ controlled_access_terms.icons: css: theme: - css/controlled_access_terms.icons-min.css: {} + css/controlled_access_terms.icons.css: {} \ No newline at end of file diff --git a/controlled_access_terms.module b/controlled_access_terms.module index 7ef2fea..cbcf2fa 100644 --- a/controlled_access_terms.module +++ b/controlled_access_terms.module @@ -7,6 +7,7 @@ use Drupal\jsonld\Normalizer\NormalizerBase; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Field\FieldTypeCategoryManagerInterface; use Drupal\controlled_access_terms\EDTFConverter; use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldConfig; @@ -111,6 +112,27 @@ function controlled_access_terms_form_field_ui_field_storage_add_form_alter(&$fo $form['#attached']['library'][] = 'controlled_access_terms/controlled_access_terms.icons'; } +/** + * Implements hook_field_info_entity_type_ui_definitions_alter(). + */ +function controlled_access_terms_field_info_entity_type_ui_definitions_alter(array &$ui_definitions, string $entity_type_id) { + // Put the generic typed relation field last and name it 'Other'. + if (isset($ui_definitions['typed_relation'])) { + $ui_definitions['typed_relation']['label'] = t('Other'); + $ui_definitions['typed_relation']['weight'] = 10; + } + // Undo some shenanigans done by the Media module to the media option. + if (isset($ui_definitions['field_ui:typed_relation:media'])) { + $ui_definitions['field_ui:typed_relation:media']['category'] = 'typed_relation'; + $ui_definitions['field_ui:typed_relation:media']['description'] = 'A reference field with a configurable type selector.'; + $ui_definitions['field_ui:typed_relation:media']['weight'] = 0; + } + // Put taxonomy term first in the option list. + if (isset($ui_definitions['field_ui:typed_relation:taxonomy_term'])) { + $ui_definitions['field_ui:typed_relation:taxonomy_term']['weight'] = -10; + } +} + /** * Update EDTF fields from the 2012 draft to match the 2018 spec. */ diff --git a/css/controlled_access_terms.icons-min.css b/css/controlled_access_terms.icons-min.css index 67c7908..e4df617 100644 --- a/css/controlled_access_terms.icons-min.css +++ b/css/controlled_access_terms.icons-min.css @@ -9,3 +9,4 @@ .field-icon-edtf.field-option__icon { background-image: url('data:image/svg+xml,'); } + diff --git a/src/Plugin/Field/FieldType/TypedRelation.php b/src/Plugin/Field/FieldType/TypedRelation.php index baedcaa..e5f2dba 100644 --- a/src/Plugin/Field/FieldType/TypedRelation.php +++ b/src/Plugin/Field/FieldType/TypedRelation.php @@ -15,7 +15,7 @@ * label = @Translation("Typed Relation"), * module = "controlled_access_terms", * category = "typed_relation", - * description = @Translation("Implements a typed relation field"), + * description = @Translation("A reference field with a configurable type selector."), * default_formatter = "typed_relation_default", * default_widget = "typed_relation_default", * list_class = "\Drupal\Core\Field\EntityReferenceFieldItemList",