2

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 &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);

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

    return $element;
  }
}
3

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",

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.