Extending Entityreference Field Class to contain additional input fields I am currently trying to build a custom field consisting of an EntityReference Input Field (Autocomplete Widget) and two textfields to add additional information in Drupal 8beta11.

I used the code from the example module and the EntityReference field type. I copied it into my custom module. Everything worked.

But then I remembered the 'new' OOP approach of Drupal 8. Why copy, when I can extend? Extending the EntityReferenceItem Class worked without any problems. The table was correctly created.

Then I tried to extend the autocomplete widget class EntityReferenceAutocompleteWidget to show my additional two textfields. Here it somehow doesn't work. The Error I get:

Fatal error: Call to undefined method Drupal\Core\Field\FieldItemList::referencedEntities() in ...\core\lib\Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget.php on line 95


I know it means more or less, that somehow the $items, which are put into the parent::formElement method doesn't have the function referencedEntities implemented. But how can I fix this?

I tried to look into other modules and even found Drupal\Core\Field\Plugin\Field\FieldWidget\AutocompleteTagsWidget to extend the EntityReferenceAutocompleteWidget, but I don't find the difference.

This is my CompoundReferenceWidget. I am grateful for any advice or hint to get it working. Or to get the information, that it can't be done the way I tried it.

/**
 * @file
 * Contains \Drupal\compound_reference\Plugin\field\widget\CompoundReferenceWidget.
 */

namespace Drupal\compound_reference\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget;
/**
 * Plugin implementation of the 'compound_reference_widget' widget.
 *
 * @FieldWidget(
 *   id = "compound_reference_widget",
 *   module = "compound_reference",
 *   label = @Translation("Modeling a reference with additional fields."),
 *   field_types = {
 *     "compound_reference"
 *   }
 * )
 */
class CompoundReferenceWidget extends EntityReferenceAutocompleteWidget {

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &amp;$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);

    $element['description'] = array(
    //$element += array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('Description of the reference.'),
      '#default_value' =&gt; isset($items[$delta]-&gt;description) ? $items[$delta]-&gt;description : NULL,
      //'#min' =&gt; 1,
      //'#required' =&gt; TRUE,
    );
    $element['start'] = array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('Start Date'),
      '#default_value' =&gt; isset($items[$delta]-&gt;start) ? $items[$delta]-&gt;start : NULL,
      '#description' =&gt; t('Start'),
    );

    return $element;
  }
}
The error is on the field item list class.

What you are missing is the list_class definition in the annotation of your field type, for anything that's an entity reference, you need to explicitly specify it, since annotations aren't inherited. See EntityReferenceItem:

*   list_class = "\Drupal\Core\Field\EntityReferenceFieldItemList",