Hi, I've noticed a issue with Map field in ajax calls

Steps to reproduce:

1. Create a custom field type that have an Map property
2. Attach this field type on one entity
3. Attach also a file definition on this entity
4. Save a new entity with data inside the map field
5. Try to edit that entity and attach something on the file field, it will return this error

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

guilopes created an issue. See original summary.

guilopes’s picture

guilopes’s picture

Status: Active » Needs review
Berdir’s picture

Do you have a backtrace for that error?

guilopes’s picture

yeah, this backtrace is on Drupal 8.2.8, but the same error should be on Drupal 8.4.x-dev because the validation still the same.

"
An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /US/en/admin/pim/product/19501/edit?element_parents=downloads/widget&ajax_form=1
StatusText: OK
ResponseText: 
( ! ) Fatal error: Call to undefined method Drupal\Core\TypedData\DataDefinition::getPropertyDefinitions() in /var/www/html/docroot/core/lib/Drupal/Core/TypedData/Plugin/DataType/Map.php on line 158
Call Stack
#TimeMemoryFunctionLocation
10.0109352104{main}(  ).../index.php:0
20.0851961512Drupal\Core\DrupalKernel->handle( ???, ???, ??? ).../index.php:19
30.09662341888Stack\StackedHttpKernel->handle( ???, ???, ??? ).../DrupalKernel.php:652
40.09662342000Drupal\Core\StackMiddleware\NegotiationMiddleware->handle( ???, ???, ??? ).../StackedHttpKernel.php:23
50.09662342392Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle( ???, ???, ??? ).../NegotiationMiddleware.php:50
60.09662342824Drupal\page_cache\StackMiddleware\PageCache->handle( ???, ???, ??? ).../ReverseProxyMiddleware.php:47
70.09672343448Drupal\page_cache\StackMiddleware\PageCache->pass( ???, ???, ??? ).../PageCache.php:78
80.09672343528Drupal\Core\StackMiddleware\KernelPreHandle->handle( ???, ???, ??? ).../PageCache.php:99
90.10542805856Drupal\Core\StackMiddleware\Session->handle( ???, ???, ??? ).../KernelPreHandle.php:47
100.10872965104Symfony\Component\HttpKernel\HttpKernel->handle( ???, ???, ??? ).../Session.php:57
110.10882966088Symfony\Component\HttpKernel\HttpKernel->handleRaw( ???, ??? ).../HttpKernel.php:64
120.34779229248call_user_func_array:{/var/www/html/vendor/symfony/http-kernel/HttpKernel.php:144}
( ???, ??? ).../HttpKernel.php:144
130.34779229448Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}(  ).../HttpKernel.php:144
140.34779229696Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext( ???, ??? ).../EarlyRenderingControllerWrapperSubscriber.php:97
150.34789241664Drupal\Core\Render\Renderer->executeInRenderContext( ???, ??? ).../EarlyRenderingControllerWrapperSubscriber.php:124
160.34789242112Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}(  ).../Renderer.php:574
170.34789242160call_user_func_array:{/var/www/html/docroot/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php:123}
( ???, ??? ).../EarlyRenderingControllerWrapperSubscriber.php:123
180.34799242856Drupal\Core\Controller\FormController->getContentResult( ???, ??? ).../EarlyRenderingControllerWrapperSubscriber.php:123
190.35439636880Drupal\Core\Form\FormBuilder->buildForm( ???, ??? ).../FormController.php:74
200.841219711520Drupal\Core\Form\FormBuilder->processForm( ???, ???, ??? ).../FormBuilder.php:314
211.963124707600Drupal\Core\Form\FormValidator->validateForm( ???, ???, ??? ).../FormBuilder.php:571
221.963224708008Drupal\Core\Form\FormValidator->doValidateForm( ???, ???, ??? ).../FormValidator.php:119
232.058825123688Drupal\Core\Form\FormValidator->executeValidateHandlers( ???, ??? ).../FormValidator.php:270
242.058825124936call_user_func_array:{/var/www/html/docroot/core/lib/Drupal/Core/Form/FormValidator.php:83}
( ???, ??? ).../FormValidator.php:83
252.058825125240Drupal\Core\Entity\ContentEntityForm->validateForm( ???, ??? ).../FormValidator.php:83
262.110925690280Drupal\Core\Entity\ContentEntityBase->validate(  ).../ContentEntityForm.php:96
272.110925690392Drupal\Core\TypedData\TypedData->validate(  ).../ContentEntityBase.php:373
282.121625795192Drupal\Core\TypedData\Validation\RecursiveValidator->validate( ???, ???, ??? ).../TypedData.php:135
292.126126008416Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validate( ???, ???, ???, ??? ).../RecursiveValidator.php:90
302.126226008968Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode( ???, ???, ??? ).../RecursiveContextualValidator.php:99
312.787428878072Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode( ???, ???, ??? ).../RecursiveContextualValidator.php:147
322.787828881072Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode( ???, ???, ??? ).../RecursiveContextualValidator.php:147
332.788728884912Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode( ???, ???, ??? ).../RecursiveContextualValidator.php:147
342.789228886920Drupal\Core\TypedData\Plugin\DataType\Map->getIterator(  ).../RecursiveContextualValidator.php:146
352.789228887384Drupal\Core\TypedData\Plugin\DataType\Map->getProperties( ??? ).../Map.php:181
"
guilopes’s picture

any update here?

guilopes’s picture

Is someone responsible for this component? If so, What It is missing to be merged?

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Joao Sausen’s picture

I have the same problem here, in my case I have created a FieldType with a dataDefinition of type Map, when creating that field in a node, or any other entity, the same error happened to me.
The patch on #2 works here.

amateescu’s picture

Category: Bug report » Support request
Priority: Major » Normal
Status: Needs review » Postponed (maintainer needs more info)

@guilopes and @Joao Sausen, how are you defining the map property in your custom field type?

If the answer is DataDefinition::create('map'), that's why you are getting this exception. Map properties have to be defined with MapDataDefinition::create().

See \Drupal\link\Plugin\Field\FieldType\LinkItem::propertyDefinitions() for an example.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

kpv’s picture

Can we get this point documented? Because it is really confusing that even though there is a DataDefinition::create($type) method it can't be used to create data of some Data Types, map specifically. What should be documented is the reason WHY can't it be used, since DataDefinition::create($type) can be mistakenly thought as a general purpose way to create data of ANY type.
Also if it is not so, shouldn't it throw a warning then?

guilopes’s picture

Status: Postponed (maintainer needs more info) » Needs work

if it's not suppose to create with DataDefinition::create('map') it should generate an exception at least, not create the field and not work properly. Is it make sense?

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

whikloj’s picture

I am trying to create a single computed field and have hit this same error.

Error: Call to undefined method Drupal\\Core\\TypedData\\DataDefinition::getPropertyDefinitions() in /var/www/html/drupal/web/core/lib/Drupal/Core/Field/FieldItemBase.php on line 50

Seemingly because there is no getPropertyDefinitions() method defined in the DataDefinitionInterface.

I was trying to extend a simple URI as the FieldType, and in the propertyDefinitions set the class and computed

public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    if (!isset(static::$propertyDefinitions)) {
        static::$propertyDefinitions = [];
        static::$propertyDefinitions['value'] = DataDefinition::create('uri')
            ->setLabel(t('Gemini Link value'))
            ->setRequired(TRUE)
            ->setClass(GeminiLinkedField::class)
            ->setComputed(TRUE);
    }
    return static::$propertyDefinitions;
}

But it always explodes because the DataDefinition doesn't have that method.
Am I missing something obvious here?

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Aporie’s picture

I stepped into the same issue, only in my case it's for a field type with an DataDefinition "entity reference".

Trying to bypass the issue using #10 comment leads me to another issue:

$properties['target_id'] = DataReferenceDefinition::create('entity:my_custom_entity')
      ->setRequired(TRUE);

got suffixed by the create function with _reference, but this plugin doesn't exist:

Drupal\Core\TypedData\DataReferenceDefinition::create($target_data_type) {
    // This assumes implementations use a "TYPE_reference" naming pattern.
    $definition = parent::create($target_data_type . '_reference');
    return $definition->setTargetDefinition(\Drupal::typedDataManager()->createDataDefinition($target_data_type));
  }

\Drupal::typedDataManager()->createDataDefinition('entity:my_custom_entity') --> returns a data definition.
\Drupal::typedDataManager()->createDataDefinition('entity:my_custom_entity_reference') --> returns "plugin not found".

The workaround:

$properties['target_id'] = DataReferenceDefinition::create('entity')
      ->setTargetDefinition(\Drupal::typedDataManager()->createDataDefinition('entity:my_custom_entity'))
      ->setRequired(TRUE);

[EDIT]
Actually the proper way to do that is to directly extend Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem.
@see Drupal\file\Plugin\Field\FieldType\FileItem

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

webberly’s picture

I get the same error in d9.2 when making a compound field extending the entityreference field.
One of them is for the contrib telephone field and the second is a list of options.

$properties['telephone'] = DataDefinition::create('field_item:telephone')
->setLabel(t('Telephone'))
->setRequired(TRUE);
$properties['status'] = DataDefinition::create('field_item:list_integer')
->setLabel(t('Status'))
->setSetting('allowed_values',['0'=> 'unverified', '1'=> 'verified'] )
->setRequired(TRUE);

Berdir’s picture

That's because you can't do that. You can't make another field type a property of yours, they are different things. What you can do is copy the property definitions of them into yours. But you'll also need to duplicate all the other related code, like checking for empty, widgets and formatters as you can't have multiple properties with the same name for obvious reasons.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.