protected EntityViewBuilder::getSingleFieldDisplay($entity, $field_name, $display_options)
Gets an EntityViewDisplay for rendering an individual field.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity.
string $field_name: The field name.
string|array $display_options: The display options passed to the viewField() method.
Return value
\Drupal\Core\Entity\Display\EntityViewDisplayInterface
File
- core/lib/Drupal/Core/Entity/EntityViewBuilder.php, line 430
Class
- EntityViewBuilder
- Base class for entity view builders.
Namespace
Drupal\Core\Entity
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | protected function getSingleFieldDisplay( $entity , $field_name , $display_options ) { if ( is_string ( $display_options )) { // View mode: use the Display configured for the view mode. $view_mode = $display_options ; $display = EntityViewDisplay::collectRenderDisplay( $entity , $view_mode ); // Hide all fields except the current one. foreach ( array_keys ( $entity ->getFieldDefinitions()) as $name ) { if ( $name != $field_name ) { $display ->removeComponent( $name ); } } } else { // Array of custom display options: use a runtime Display for the // '_custom' view mode. Persist the displays created, to reduce the number // of objects (displays and formatter plugins) created when rendering a // series of fields individually for cases such as views tables. $entity_type_id = $entity ->getEntityTypeId(); $bundle = $entity ->bundle(); $key = $entity_type_id . ':' . $bundle . ':' . $field_name . ':' . hash( 'crc32b' , serialize( $display_options )); if (!isset( $this ->singleFieldDisplays[ $key ])) { $this ->singleFieldDisplays[ $key ] = EntityViewDisplay::create( array ( 'targetEntityType' => $entity_type_id , 'bundle' => $bundle , 'status' => TRUE, ))->setComponent( $field_name , $display_options ); } $display = $this ->singleFieldDisplays[ $key ]; } return $display ; } |
Please login to continue.