viewElements example

return new static($plugin_id$plugin_definition$configuration['field_definition']$configuration['settings']$configuration['label']$configuration['view_mode']$configuration['third_party_settings']);
  }

  /** * {@inheritdoc} */
  public function view(FieldItemListInterface $items$langcode = NULL) {
    // Default the language to the current content language.     if (empty($langcode)) {
      $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
    }
    $elements = $this->viewElements($items$langcode);

    // If there are actual renderable children, use #theme => field, otherwise,     // let access cacheability metadata pass through for correct bubbling.     if (Element::children($elements)) {
      $entity = $items->getEntity();
      $entity_type = $entity->getEntityTypeId();
      $field_name = $this->fieldDefinition->getName();
      $info = [
        '#theme' => 'field',
        '#title' => $this->fieldDefinition->getLabel(),
        '#label_display' => $this->label,
        

class ResponsiveImageTestFormatter extends ResponsiveImageFormatter {

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $elements = parent::viewElements($items$langcode);
    // Unset #item_attributes to test that the theme function can handle that.     foreach ($elements as &$element) {
      if (isset($element['#item_attributes'])) {
        unset($element['#item_attributes']);
      }
    }
    return $elements;
  }

}
->method('generateFromRoute')
      ->with('<none>', [][], FALSE)
      ->willReturn('http://example.com');
    $container = new ContainerBuilder();
    $container->set('plugin.manager.field.field_type', $fieldTypePluginManager);
    $container->set('url_generator', $urlGenerator);
    \Drupal::setContainer($container);
    $fieldList->setValue([$linkItem]);

    $pathValidator = $this->createMock(PathValidatorInterface::class);
    $linkFormatter = new LinkFormatter('', []$fieldDefinition[], '', '', []$pathValidator);
    $elements = $linkFormatter->viewElements($fieldList, 'es');
    $this->assertEquals('link', $elements[0]['#type']);
  }

  /** * Tests when LinkItem::getUrl throws an unexpected exception. */
  public function testFormatterLinkItemUrlUnexpectedException() {
    $exception = new \Exception('Unexpected!!!');

    $linkItem = $this->createMock(LinkItemInterface::class);
    $entity = $this->createMock(EntityInterface::class);
    

class AttachmentTestFormatter extends NumericUnformattedFormatter {

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $elements = parent::viewElements($items$langcode);

    // Add dummy attachments.     $entity_id = $items->getEntity()->id();
    $elements['#attached']['library'][] = 'foo/fake_library';
    $elements['#attached']['drupalSettings']['AttachmentIntegerFormatter'][$entity_id] = $entity_id;

    return $elements;
  }

}
Home | Imprint | This part of the site doesn't use cookies.