0

When I would like to get the values inserted in a form, I use:

$form_state->getValue('myfield');

If I would like to have some information on the field definition, I use:

$form_state->getFormObject()->getEntity()->myfield->getfieldDefinition();

Every time I check the class resulting from the following statement:

$form_state->getFormObject()->getEntity()->myfield

It is of the type Drupal\\Core\\Field\\FieldItemList which is fine for getting/setting the values

Suppose that "myfield" is of the type "language_field" defined in \Drupal\languagefield\Plugin\Field\FieldType\LanguageItem.

This class have a public function

public function getNativeName()

To get the full name of the language.

But My question is: how to access this function fromwithin the FieldItemList object? (Same problem arise with many other field type)

Added info:

With the help of @Eyal, I could access my object method:

$form_state->getFormObject()->getEntity()->myfield->get($delta)->getNativeName()‌​;

But this is valid only if the entity is already saved.

In the case of a new (or edited) item, the entity doesn't store the new value. Here is the code for what I am trying to explain:

The 2 variables will have the same value if I open and save the form without change, but this will not be the case if I update the item at delta=1.

function my_validate($form, FormStateInterface $form_state) {
  $val_by_entity=$form_state->getFormObject()->getEntity()->myfield->get(1)->value;
  $val_by_form_state_value=$form_state->getValue(['myfield', 1, 'value']);
}
1

You first need to understand that the entity object in question does not have a property called myfield.

Drupal uses something called magic properties to mask the get method required for retrieving the field item list. See FieldableEntityInterface::get() for more information.

The second thing you should know, is that drupal treats all fields as multi value. That means that a field that is defined to have a single value would still be represented a list with a single item.

Finally, to answer your question, you can either use the FieldItemListInterface::get() method to get the field item object, it you can access it as though it was an array.

$entity->get('myfield')->get(0);

OR

$entity->myfield[0];

  • This is a really nice answer: I could access my LanguageItem object by using $form_state->getFormObject()->getEntity()->myfield>get($delta)->getNativeName(); – Baud Jun 24 '16 at 7:54
  • Thanks :) , I try to avoid magic properties because I enjoy the type hinting provided by the IDE. When I write $entity->myfield the IDE doesn't know what is the type returned by the magic property. – Eyal Jun 24 '16 at 7:57
  • Unfortunately, one issue is remaining: if $delta is a newly added item (or changed item), its value can be obtained with $form_state->getValue('myfield')[$delta]['value']; but the FormObject doesn't contains yet this new value. Thus I can't use getNativeName() for this new item – Baud Jun 24 '16 at 8:10
  • It all depends on the context. After the buildEntity method runs, the updated values are set on the entity. FYI the getValue method of form_state can accept arrays $form_state->getValue(['myfield', $delta, 'value']); the advantage is that if it is not found no exception is thrown. – Eyal Jun 24 '16 at 9:20
  • Thank you for the tip! The context is form_validate and/or form_submit. – Baud Jun 24 '16 at 9:40

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.