4

I am creating a custom field widget for an entity_reference field. This has a custom select field and an Ajax Submit button. Once the user selects an option (entity) from the select_field and submits it, I am adding the entity in a dragTable below.

I could achieve that until now, but my need is to add a custom label for the entity selected. I provided a textfield to capture the custom values and I am able to fetch the values. But, I am unable to save this custom text_field data. I am extending the core WidgetBase class for my custom field Widget. For an entity_reference field, is it possible to save custom label parameters along with the entities?

enter image description here

Updated: After enabling fieldType, duplicate results are appearing while creating field. Any help on this? enter image description here.

1 Answer 1

5

You need to create a custom field type that can hold the value along with the entity reference.

A field widget can not store any data that the field type has not provided space for.

You must create a new field type would look similar to this:

<?php

namespace Drupal\MY_MODULE\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;

/**
 * @FieldType(
 *   id = "entity_reference_with_value",
 *   label = @Translation("Entity reference with value"),
 *   category = @Translation("Reference"),
 *   default_widget = "my_custom_field_widget",
 *   default_formatter = "some_formatter",
 *   list_class = "\Drupal\Core\Field\EntityReferenceFieldItemList",
 * )
 */
class EntityReferenceWithValue extends EntityReferenceItem {

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

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties = parent::propertyDefinitions($field_definition);
    $properties['value'] = DataDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Value'))
      ->setRequired(TRUE);

    return $properties;
  }

}

Now you can get the value with $entity->field_name->value.

It would also be better to extend EntityReferenceAutocompleteWidget in your field widget, instead of WidgetBase. Then you can just do like this in the formElement method:

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  $widget = parent::formElement($items, $delta, $element, $form, $form_state);

  $widget['value'] = [
    '#type' => 'textfield',
    '#title' => $this->t('Value'),
    '#default_value' => $items[$delta]->value,
    '#weight' => 999,
  ];

  return $widget;
}
3
  • Do we need to do any changes in field_formatter to save the field_value or will it save automatically?
    – Kiran
    Nov 22, 2019 at 11:52
  • Do you mean in the field widget? The only thing you have to make sure it that the field is keyed ['value'] in your field widget. I have updated my answer.
    – user72672
    Nov 22, 2019 at 12:00
  • when I enabled this custom fieldType, I am finding few elements repeated in "Reference" while creating field. (Attaching screenshot in ticket for your reference). Any idea why this is happening?
    – Kiran
    Nov 26, 2019 at 8:31

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.