4

I have the following code to get the value that is stored in a field

$nid = 76;
$node = \Drupal::service('entity.manager')->getStorage('node')->load($nid);
foreach ($node->field_aaaa as $delta) {      
  dpm($delta->value);
}

With this I can get the correct value let says:

 * id_string1
 * id_string2
 * id_string3

But my field has a Field Formatter named title_formatter that will take the string id_string1 and will show something like this:

String 1

In the UI this works fine if I chose my Title Formatter (title_formatter) for the field I can see the 'String' when I viewing my node.

But how to apply the Field Formatter programmatically to the field values?

2

2 Answers 2

8

The entity view builder provides a method to build a render array for a single field using a field formatter:

Drupal\Core\Entity\EntityViewBuilderInterface::viewField()

  /**
   * Builds a renderable array for the value of a single field in an entity.
   *
   * The resulting output is a fully themed field with label and multiple
   * values.
   *
   * This function can be used by third-party modules that need to output an
   * isolated field.
   * - Do not use inside node (or any other entity) templates; use
   *   render($content[FIELD_NAME]) instead.
   * - The FieldItemInterface::view() method can be used to output a single
   *   formatted field value, without label or wrapping field markup.
   *
   * The function takes care of invoking the prepare_view steps. It also
   * respects field access permissions.
   *
   * @param \Drupal\Core\Field\FieldItemListInterface $items
   *   FieldItemList containing the values to be displayed.
   * @param string|array $display_options
   *   Can be either:
   *   - The name of a view mode. The field will be displayed according to the
   *     display settings specified for this view mode in the $field
   *     definition for the field in the entity's bundle. If no display settings
   *     are found for the view mode, the settings for the 'default' view mode
   *     will be used.
   *   - An array of display options. The following key/value pairs are allowed:
   *     - label: (string) Position of the label. The default 'field' theme
   *       implementation supports the values 'inline', 'above' and 'hidden'.
   *       Defaults to 'above'.
   *     - type: (string) The formatter to use. Defaults to the
   *       'default_formatter' for the field type. The default formatter will
   *       also be used if the requested formatter is not available.
   *     - settings: (array) Settings specific to the formatter. Defaults to the
   *       formatter's default settings.
   *     - weight: (float) The weight to assign to the renderable element.
   *       Defaults to 0.
   *
   * @return array
   *   A renderable array for the field values.
   *
   * @see \Drupal\Core\Entity\EntityViewBuilderInterface::viewFieldItem()
   */
  public function viewField(FieldItemListInterface $items, $display_options = []);

To apply this method on a field object you can use $field->view() or if you only want a single value from a multivalue field without the wrapper and label you can also use it on a field item (delta).

EDIT: With this code you can find the values that you need.

$nid = 76;
$node = \Drupal::service('entity.manager')->getStorage('node')->load($nid);
foreach ($node->field_aaaa as $i => $delta) {   
  dpm($delta->value);
  dpm((string) \Drupal::service('renderer')->render($delta->view(array('type' => 'title_formatter'))));
}
4
  • thanks for your help, I found the correct way, and I will add the code to your answer ;-) Jul 5, 2017 at 16:50
  • Hi 4k4, I know there is a steel learning curve for Drupal when comparing to others. But how should I find these methods and properties from a whole lot of array? For me, it is really hard to find which is the correct way to do. For example, If I have a decimal field (price) with prefix, then to get the price including the prefix I often find it is easier to concatenate the value with prefix than finding the above methods. If you can talk about how to approach in the scenario then that will be a great info for the beginners and intermediates.
    – Harish ST
    Jul 2, 2020 at 13:42
  • 1
    @HarishST, see drupal.stackexchange.com/a/308254/47547
    – 4uk4
    Nov 16, 2021 at 21:11
  • @4k4 Thank you!
    – Harish ST
    Nov 17, 2021 at 18:07
3

I'm sure that this is not the best option but at least it works.

Go to admin/structure/types/manage/YOUR_CONTENT_TYPE/display and select the Title Formatter for your field, and then use this code:

$nid = 76;
$node = \Drupal::service('entity.manager')->getStorage('node')->load($nid);
foreach ($node->field_aaaa as $i => $delta) {   
  dpm($delta->value);
  dpm($node->field_aaaa->view('full')[$i]['#markup']);
}

You will see this:

* id_string1
* String 1
* id_string2
* String 2
* id_string3
* String 3

EDIT: The correct way using the @4k4 answer is:

$nid = 76;
$node = \Drupal::service('entity.manager')->getStorage('node')->load($nid);
foreach ($node->field_aaaa as $i => $delta) {   
  dpm($delta->value);
  dpm((string) \Drupal::service('renderer')->render($delta->view(array('type' => 'title_formatter'))));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.