0

I have a very simple custom field/widget/formatter set.

YoutubeLink.php (custom field):

<?php
namespace Drupal\ad_general\Plugin\FIeld\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
 * Defines the 'boolean' entity field type.
 *
 * @FieldType(
 *   id = "youtube",
 *   label = @Translation("Youtube link"),
 *   description = @Translation("Youtube link field."),
 *   default_widget = "youtube_default",
 *   default_formatter = "youtube_default",
 * )
 */
class YoutubeLink extends FieldItemBase {

  public function isEmpty() {
    $value = $this->get('value')->getValue();
    return $value === NULL || $value === '';
  }

  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['value'] = DataDefinition::create('string')->setLabel(t('Youtube link'));
    return $properties;
  }

  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return [
      'columns' => [
        'value' => [
          'type' => 'varchar',
          'length' => 255,
        ],
      ],
    ];
  }
}

YoutubeLinkWidget.php (custom widget):

<?php

namespace Drupal\ad_general\Plugin\FIeld\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'text_textarea' widget.
 *
 * @FieldWidget(
 *   id = "youtube_default",
 *   label = @Translation("Youtube default"),
 *   field_types = {
 *     "youtube"
 *   }
 * )
 */
class YoutubeLinkWidget extends WidgetBase {

  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $elements = [];

    $elements['value'] = [
      '#type' => 'textfield',
      '#title' => 'Enter Youtube video link.',
    ];

    return $elements;
  }
}

YoutubeLinkFormatter.php (custom formatter):

<?php

namespace Drupal\ad_general\Plugin\FIeld\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;

/**
 * @FieldFormatter(
 *   id = "youtube_default",
 *   label = @Translation("Youtube link"),
 *   description = @Translation("Display a link to the video."),
 *   field_types = {
 *     "youtube"
 *   }
 * )
 */
class YoutubeLinkFormatter extends FormatterBase {

  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($items as $delta => $item) {
      $elements[$delta] = [
        '#type' => 'markup',
        '#markup' => $item,
      ];
    }

    return $elements;
  }
}

I added this field to a content type. Whenever I want to add new content of this type, I now get the following error:

Youtube link The website encountered an unexpected error. Please try again later. Recoverable fatal error: Object of class Drupal\ad_general\Plugin\FIeld\FieldType\YoutubeLink could not be converted to string in Drupal\Component\Utility\Xss::filter() (line 67 of core/lib/Drupal/Component/Utility/Xss.php). Drupal\Component\Utility\Xss::filter(Object, Array) (Line: 753) Drupal\Core\Render\Renderer->ensureMarkupIsSafe(Array) (Line: 384) Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195) Drupal\Core\Render\Renderer->render(Array) (Line: 490) Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 128) __TwigTemplate_5b03bca200df2d5ba1ca47d2e222b86347639abbdcd0d8d587ebad065efba4c8->doDisplay(Array, Array) (Line: 432) Twig_Template->displayWithErrorHandling(Array, Array) (Line: 403) Twig_Template->display(Array) (Line: 411) Twig_Template->render(Array) (Line: 64) twig_render_template('core/themes/classy/templates/field/field.html.twig', Array) (Line: 384) Drupal\Core\Theme\ThemeManager->render('field', Array) (Line: 437) Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195) Drupal\Core\Render\Renderer->render(Array) (Line: 490) Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 87) __TwigTemplate_427ceffeef7118469d7753956e2b0d458f0c2f44d3b385e4ec36128cfc86801f->block_content(Array, Array) (Line: 215) Twig_Template->displayBlock('content', Array, Array) (Line: 72) __TwigTemplate_427ceffeef7118469d7753956e2b0d458f0c2f44d3b385e4ec36128cfc86801f->doDisplay(Array, Array) (Line: 432) Twig_Template->displayWithErrorHandling(Array, Array) (Line: 403) Twig_Template->display(Array) (Line: 411) Twig_Template->render(Array) (Line: 64) twig_render_template('core/themes/bartik/templates/block.html.twig', Array) (Line: 384) Drupal\Core\Theme\ThemeManager->render('block', Array) (Line: 437) Drupal\Core\Render\Renderer->doRender(Array) (Line: 450) Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195) Drupal\Core\Render\Renderer->render(Array) (Line: 490) Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 56) __TwigTemplate_6bc2c2a4be03937decaa4eb1c0d39af747b44d5c7e30912d8db85c3fa5c28de6->doDisplay(Array, Array) (Line: 432) Twig_Template->displayWithErrorHandling(Array, Array) (Line: 403) Twig_Template->display(Array) (Line: 411) Twig_Template->render(Array) (Line: 64) twig_render_template('core/themes/stable/templates/layout/layout--onecol.html.twig', Array) (Line: 384) Drupal\Core\Theme\ThemeManager->render('layout__onecol', Array) (Line: 437) Drupal\Core\Render\Renderer->doRender(Array) (Line: 450) Drupal\Core\Render\Renderer->doRender(Array) (Line: 450) Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195) Drupal\Core\Render\Renderer->render(Array) (Line: 490) Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 114) __TwigTemplate_4ae59022820823361d314fdb0db03eda142ac3295eb1399a7323e963e3d3bf5a->doDisplay(Array, Array) (Line: 432) Twig_Template->displayWithErrorHandling(Array, Array) (Line: 403) Twig_Template->display(Array) (Line: 411) Twig_Template->render(Array) (Line: 64) twig_render_template('core/themes/bartik/templates/node.html.twig', Array) (Line: 384) Drupal\Core\Theme\ThemeManager->render('node', Array) (Line: 437) Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195) Drupal\Core\Render\Renderer->render(Array, ) (Line: 226) Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent{closure}() (Line: 582) Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227) Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117) Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90) Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) call_user_func(Array, Object, 'kernel.view', Object) (Line: 111) Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156) Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68) Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57) Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47) Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99) Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78) Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47) Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52) Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23) Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 666) Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Does anyone have an idea what is causing this issue? Thank you very much for any sort of help.

  • $item is likely an object and not a string. – Kevin Jul 14 '18 at 11:10
0

The problem was with the #markup property of the formatter - $item is indeed just an object, so I had to convert it to a string by using the getString() method (as an example).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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