From f6d9ad4754924de1661d995249417f64fa4bc155 Mon Sep 17 00:00:00 2001 From: Eric Bremner <ebremner@uwaterloo.ca> Date: Fri, 14 Oct 2022 09:47:01 -0400 Subject: [PATCH] ISTWCMS-5880: adding allowed values function for type of media field --- ...ld.storage.node.field_uw_type_of_media.yml | 7 ++--- uw_cfg_common.module | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/config/install/field.storage.node.field_uw_type_of_media.yml b/config/install/field.storage.node.field_uw_type_of_media.yml index 3671c93b..196be0af 100644 --- a/config/install/field.storage.node.field_uw_type_of_media.yml +++ b/config/install/field.storage.node.field_uw_type_of_media.yml @@ -9,11 +9,8 @@ field_name: field_uw_type_of_media entity_type: node type: list_string settings: - allowed_values: - - - value: image - label: Image - allowed_values_function: '' + allowed_values: { } + allowed_values_function: '_uw_cfg_common_allowed_media_types' module: options locked: false cardinality: 1 diff --git a/uw_cfg_common.module b/uw_cfg_common.module index 0cd525a8..0772ca8d 100644 --- a/uw_cfg_common.module +++ b/uw_cfg_common.module @@ -7,6 +7,7 @@ use Drupal\Component\Utility\Html; use Drupal\Core\Access\AccessResult; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; @@ -14,6 +15,7 @@ use Drupal\Core\Link; use Drupal\Core\Render\Element; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\fillpdf\Controller\HandlePdfController; use Drupal\fillpdf\Form\FillPdfFormForm; use Drupal\media_library\MediaLibraryState; @@ -1432,3 +1434,29 @@ function uw_cfg_common_preprocess_html(&$variables): void { $variables['page']['#attached']['html_head'][] = [$noindex, 'noindex']; } } + +/** + * Set dynamic allowed values for the type of media field. + * + * @param \Drupal\field\Entity\FieldStorageConfig $definition + * The field definition. + * @param \Drupal\Core\Entity\ContentEntityInterface|null $entity + * The entity being created if applicable. + * @param bool $cacheable + * Boolean indicating if the results are cacheable. + * + * @return array + * An array of possible key and value options. + * + * @see options_allowed_values() + */ +function _uw_cfg_common_allowed_media_types( + FieldStorageConfig $definition, + ContentEntityInterface $entity = NULL, $cacheable +) { + + return [ + 'banner' => 'Banner images', + 'image' => 'Image', + ]; +} -- GitLab