viewField example

      // using it if have no parameters.       $result[$delta] = $args ? call_user_func_array([$item$method]$args) : $item->{$method}();
    }
    return $result;
  }

  /** * {@inheritdoc} */
  public function view($display_options = []) {
    $view_builder = \Drupal::entityTypeManager()->getViewBuilder($this->getEntity()->getEntityTypeId());
    return $view_builder->viewField($this$display_options);
  }

  /** * {@inheritdoc} */
  public function generateSampleItems($count = 1) {
    $field_definition = $this->getFieldDefinition();
    $field_type_class = $field_definition->getItemDefinition()->getClass();
    for ($delta = 0; $delta < $count$delta++) {
      $values[$delta] = $field_type_class::generateSampleValue($field_definition);
    }
    
    $main_entity = $this->createTestEntity('entity_test_mul');
    $main_entity->addTranslation('es', $main_entity->getTranslation('en')->toArray());
    $main_entity->set('reference_field', $referenced_entity);

    $view_builder = $this->container->get('entity_type.manager')->getViewBuilder('entity_test_mul');
    $renderer = $this->container->get('renderer');

    // Build the view for the reference field and render in English - the site     // default. Confirm the reference field shows the content of the English     // translation.     $reference_field = $main_entity->get('reference_field');
    $reference_field_array_english = $view_builder->viewField($reference_field, 'full');
    $rendered_reference_field_english = $renderer->renderRoot($reference_field_array_english);
    $this->assertStringContainsString('Text in English', (string) $rendered_reference_field_english);

    // Change the default language to Spanish and render the reference     // field again. It should display the contents of the Spanish translation.     \Drupal::service('language.default')->set($es);
    \Drupal::languageManager()->reset();
    \Drupal::languageManager()->getCurrentLanguage();
    $reference_field_array_spanish = $view_builder->viewField($reference_field, 'full');
    $rendered_reference_field_spanish = $renderer->renderRoot($reference_field_array_spanish);
    $this->assertStringContainsString('Text in Spanish', (string) $rendered_reference_field_spanish);
  }

  public function viewFieldItem(FieldItemInterface $item$display = []) {
    $entity = $item->getEntity();
    $field_name = $item->getFieldDefinition()->getName();

    // Clone the entity since we are going to modify field values.     $clone = clone $entity;

    // Push the item as the single value for the field, and defer to viewField()     // to build the render array for the whole list.     $clone->{$field_name}->setValue([$item->getValue()]);
    $elements = $this->viewField($clone->{$field_name}$display);

    // Extract the part of the render array we need.     $output = $elements[0] ?? [];
    if (isset($elements['#access'])) {
      $output['#access'] = $elements['#access'];
    }

    return $output;
  }

  /** * Gets an EntityViewDisplay for rendering an individual field. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. * @param string $field_name * The field name. * @param string|array $display_options * The display options passed to the viewField() method. * * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface */
Home | Imprint | This part of the site doesn't use cookies.