Where the error appears: JSONAPI output

How to reproduce: Take any entity output and change a timestamp property (changed, created) to use the "Date Time (Date Time field)" enhancer.

Result: Cannot access that entity via JSONAPI due to the above error.

Also, after going back to the regular site, this notice is thrown once:

Notice: Use of undefined constant DATETIME_STORAGE_TIMEZONE - assumed 'DATETIME_STORAGE_TIMEZONE' in Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\DateTimeFromStringEnhancer->Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\{closure}() (line 26 of modules/contrib/jsonapi_extras/src/Plugin/jsonapi/FieldEnhancer/DateTimeFromStringEnhancer.php).

Screenshot attached shows the field enhancer configuration.

Running PHP 7.0.33.

Comments

ivi.arocom created an issue. See original summary.

yoruvo’s picture

The problem lies with having the Date Time core module disabled (it's optional). This means that the constant used in that file is never defined.

Also, that constant is deprecated anyway; Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::STORAGE_TIMEZONE should be used.

It begs the question how to handle this, as ideally the field enhancer should not be available with the datetime module disabled.

sergiu stici’s picture

Status: Active » Needs review
StatusFileSize
new1.56 KB

I provided a patch. Please review.

sean_e_dietrich’s picture

Status: Needs review » Needs work

The provided patch does not work if DateTime is not installed. Taking it back to @ivi.arocom I believe that this is a good idea to make it so there is a dependency annotation. That would include an array of modules that must be installed before these can be used.

Once applying patch get the following error

The website encountered an unexpected error. Please try again later.
Error: Class 'Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface' not found in Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\DateTimeFromStringEnhancer->Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\{closure}() (line 27 of modules/contrib/jsonapi_extras/src/Plugin/jsonapi/FieldEnhancer/DateTimeFromStringEnhancer.php).
Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\DateTimeFromStringEnhancer->Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\{closure}('2019-04-12T17:10:36+00:00') (Line: 37)
Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\DateTimeFromStringEnhancer->doUndoTransform('2019-04-12T17:10:36+00:00', Object) (Line: 57)
Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase->undoTransform('2019-04-12T17:10:36+00:00') (Line: 59)
Drupal\jsonapi_extras\Normalizer\FieldItemNormalizer->normalize(Object, 'api_json', Array) (Line: 143)
Symfony\Component\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 62)
Drupal\jsonapi\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 93)
Drupal\jsonapi\Normalizer\FieldNormalizer->normalizeFieldItems(Object, 'api_json', Array) (Line: 33)
Drupal\jsonapi\Normalizer\FieldNormalizer->normalize(Object, 'api_json', Array) (Line: 143)
Symfony\Component\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 62)
Drupal\jsonapi\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 93)
Drupal\jsonapi\Normalizer\ResourceObjectNormalizer->serializeField(Object, Array, 'api_json') (Line: 59)
Drupal\jsonapi\Normalizer\ResourceObjectNormalizer->normalize(Object, 'api_json', Array) (Line: 38)
Drupal\jsonapi_extras\Normalizer\JsonApiNormalizerDecoratorBase->normalize(Object, 'api_json', Array) (Line: 23)
Drupal\jsonapi_extras\Normalizer\ResourceObjectNormalizer->normalize(Object, 'api_json', Array) (Line: 143)
Symfony\Component\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 62)
Drupal\jsonapi\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 26)
Drupal\jsonapi\Normalizer\DataNormalizer->Drupal\jsonapi\Normalizer\{closure}(Object)
array_map(Object, Array) (Line: 27)
Drupal\jsonapi\Normalizer\DataNormalizer->normalize(Object, 'api_json', Array) (Line: 143)
Symfony\Component\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 62)
Drupal\jsonapi\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 198)
Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer->normalize(Object, 'api_json', Array) (Line: 143)
Symfony\Component\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 62)
Drupal\jsonapi\Serializer\Serializer->normalize(Object, 'api_json', Array) (Line: 120)
Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber->renderResponseBody(Object, Object, Object, 'api_json') (Line: 85)
Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber->onResponse(Object, 'kernel.response', Object)
call_user_func(Array, Object, 'kernel.response', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.response', Object) (Line: 191)
Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object, Object, 1) (Line: 173)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
sean_e_dietrich’s picture

Status: Needs work » Needs review
Related issues: +#3047728: Allow for enhancers to require dependencies in order to be usable.
StatusFileSize
new1.54 KB

Previous patch didn't work for me. Uploading new patch.

This is also contingent that the DateTime module is enabled.

https://www.drupal.org/project/jsonapi_extras/issues/3047728

Marking Needs Review.

cindytwilliams’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new20.56 KB
new52.59 KB
new60.69 KB

I tested this patch (#5), and it works as described.

With the datetime module enabled, I changed a timestamp property to use the "Date Time (Date Time field)" enhancer. It saved successfully with no errors.

e0ipso’s picture

Status: Reviewed & tested by the community » Fixed

Fixed! Thanks for contributing.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.