Skip to main content

Drupal ^8: How to get list item text programmatically

//Check if the field value is set and not empty
  if ($entity->hasField('field_custom') && !$entity->get('field_custom')->isEmpty()) {
    $entity->get('field_custom')->getValue(); //returns the array key value e.g ['alpha'=>'Alpha letter', 'beta' => 'beta letter'] will return alpha or beta 
    $entity->field_custom->getSetting('allowed_values'); // returns the whole list text array e.g. ['alpha'=>'Alpha letter', 'beta' => 'beta letter' ]
    $entity->field_custom->getSetting('allowed_values')[$entity->field_custom->value]; //Returns the array selected value - name not the array key so for example if the value is beta then it will return beta letter for the array ['alpha'=>'Alpha letter', 'beta' => 'beta letter'] and the beta as the selected value
  }

 

entity

Latest Articles

Using drupal/core-composer-scaffold to prevent rewriting .htaccess or robots.txt file admin_pixelthi… Sun, 12/01/2024 - 16:49 Using drupal/core-composer-scaffold to prevent rewriting .htaccess or robots.txt file

Using drupal/core-composer-scaffold to prevent rewriting .htaccess or robots.txt file

composer
Controlling ckeditor styling options with hook_editor_js_settings_alter admin_pixelthi… Thu, 05/16/2024 - 10:49 Controlling ckeditor styling options with hook_editor_js_settings_alter

Controlling ckeditor styling options with hook_editor_js_settings_alter

php hook hook_editor_js_settings_alter hook_field_widget_single_[WIDGET_TYPE]_form_alter
Upgrading Drupal 9 Modules for Drupal 10: A Simple Guide admin_pixelthi… Tue, 09/05/2023 - 14:18 Upgrading Drupal 9 Modules for Drupal 10: A Simple Guide

Upgrading Drupal 9 Modules for Drupal 10: A Simple Guide

composer drupal9 drupal10