From b6b9f500527134d4b718ddd556ee2acaa27b73f2 Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Wed, 26 Jun 2024 16:40:42 -0300 Subject: [PATCH] Add SVG icons to field types. (#126) --- ...led_access_terms.field_type_categories.yml | 6 ++++ controlled_access_terms.libraries.yml | 4 +++ controlled_access_terms.module | 34 +++++++++++++++++-- css/controlled_access_terms.icons-min.css | 12 +++++++ src/Plugin/Field/FieldType/AuthorityLink.php | 2 +- src/Plugin/Field/FieldType/TypedRelation.php | 4 +-- 6 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 controlled_access_terms.field_type_categories.yml create mode 100644 controlled_access_terms.libraries.yml create mode 100644 css/controlled_access_terms.icons-min.css diff --git a/controlled_access_terms.field_type_categories.yml b/controlled_access_terms.field_type_categories.yml new file mode 100644 index 0000000..cab2182 --- /dev/null +++ b/controlled_access_terms.field_type_categories.yml @@ -0,0 +1,6 @@ +typed_relation: + label: 'Typed Relation' + description: 'A Reference field, with an associated type selected from a configurable list.' + weight: 100 + libraries: + - controlled_access_terms/controlled_access_terms.icons diff --git a/controlled_access_terms.libraries.yml b/controlled_access_terms.libraries.yml new file mode 100644 index 0000000..329d531 --- /dev/null +++ b/controlled_access_terms.libraries.yml @@ -0,0 +1,4 @@ +controlled_access_terms.icons: + css: + theme: + css/controlled_access_terms.icons-min.css: {} diff --git a/controlled_access_terms.module b/controlled_access_terms.module index 302d69a..1173658 100644 --- a/controlled_access_terms.module +++ b/controlled_access_terms.module @@ -5,11 +5,11 @@ * Controlled Access Terms stuff. */ -use Drupal\jsonld\Normalizer\NormalizerBase; -use Drupal\Core\Entity\EntityInterface; use Drupal\controlled_access_terms\EDTFConverter; -use Drupal\field\Entity\FieldStorageConfig; +use Drupal\Core\Entity\EntityInterface; use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; +use Drupal\jsonld\Normalizer\NormalizerBase; /** * Implements hook_rdf_namespaces(). @@ -104,6 +104,34 @@ function controlled_access_terms_jsonld_field_mappings() { ]; } +/** + * Implements hook_form_FORM_ID_alter(). + */ +function controlled_access_terms_form_field_ui_field_storage_add_form_alter(&$form, &$form_state, $form_id) { + $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 new file mode 100644 index 0000000..e4df617 --- /dev/null +++ b/css/controlled_access_terms.icons-min.css @@ -0,0 +1,12 @@ +.field-icon-typed_relation.field-option__icon { + background-image: url('data:image/svg+xml,'); +} + +.field-icon-authority-link.field-option__icon { + background-image: url('data:image/svg+xml,'); +} + +.field-icon-edtf.field-option__icon { + background-image: url('data:image/svg+xml,'); +} + diff --git a/src/Plugin/Field/FieldType/AuthorityLink.php b/src/Plugin/Field/FieldType/AuthorityLink.php index afe3b85..1500351 100644 --- a/src/Plugin/Field/FieldType/AuthorityLink.php +++ b/src/Plugin/Field/FieldType/AuthorityLink.php @@ -14,7 +14,7 @@ * @FieldType( * id = "authority_link", * label = @Translation("Authority Link"), - * description = @Translation("Stores a URL string, an authority source dropdown, an optional varchar link text, and optional blob of attributes to assemble a link."), + * description = @Translation("A Link field, with an authority source selected from a configurable list."), * default_widget = "authority_link_default", * default_formatter = "authority_formatter_default", * constraints = { diff --git a/src/Plugin/Field/FieldType/TypedRelation.php b/src/Plugin/Field/FieldType/TypedRelation.php index 24fc0f5..e5f2dba 100644 --- a/src/Plugin/Field/FieldType/TypedRelation.php +++ b/src/Plugin/Field/FieldType/TypedRelation.php @@ -14,8 +14,8 @@ * id = "typed_relation", * label = @Translation("Typed Relation"), * module = "controlled_access_terms", - * category = @Translation("Typed Relation"), - * description = @Translation("Implements a typed relation field"), + * category = "typed_relation", + * 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",