Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content model documentation: on edit, fatal errors leading to white screen #16827

Closed
1 task
swirtSJW opened this issue Jan 11, 2024 · 8 comments
Closed
1 task
Assignees
Labels
CMS Team CMS Product team that manages both editor exp and devops Defect Something isn't working (issue type) Drupal engineering CMS team practice area Facilities Facilities products (VAMC, Vet Center, etc) sitewide

Comments

@swirtSJW
Copy link
Contributor

swirtSJW commented Jan 11, 2024

Describe the defect

On trying to edit or create a new content model document, I receive a white screen error
image

Looking at the recent log entries I see
image

TypeError: method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given in method_exists() (line 113 of /var/www/cms/docroot/modules/custom/va_gov_media/src/EventSubscriber/MediaEventSubscriber.php). [dd.trace_id=9223372036854775807 dd.span_id=721623755462565909]

The line of code was merged Jan 10 2024 as part of this alt text validation PR

The problem is the line of code
if (method_exists($fieldDefinition, 'id'))
assumes that $fieldDefinition is an object when sometimes it can be null.

To Reproduce

Steps to reproduce the behavior:

  1. Login as an admin
  2. Go to https://prod.cms.va.gov/admin/structure/cm_document/5/edit
  3. or https://prod.cms.va.gov/admin/structure/cm_document/add
  4. See error

AC / Expected behavior

We should be able to add or edit things without fatal errors.

Additional context

This is surfacing on Content Model Document entities but there is nothing special about them related to image fields. They are just another fieldable entity like nodes, terms, and paragraphs so it is possible this error is happening other places too.

ACs

  • Content model documentation can be edited / saved successfully
@swirtSJW swirtSJW added Defect Something isn't working (issue type) Needs refining Issue status labels Jan 11, 2024
@jilladams jilladams changed the title Fatal errors leading to white screen Content model documentation: Fatal errors leading to white screen Jan 11, 2024
@jilladams jilladams changed the title Content model documentation: Fatal errors leading to white screen Content model documentation: on edit, fatal errors leading to white screen Jan 11, 2024
@swirtSJW
Copy link
Contributor Author

Could be fixed by changing
if (method_exists($fieldDefinition, 'id')) {

to

if (is_object($fieldDefinition) && method_exists($fieldDefinition, 'id')) {

@swirtSJW
Copy link
Contributor Author

swirtSJW commented Jan 12, 2024

Looking into this deeper. The fix I proposed above would fix it without a problem and is worth adding the check to prevent the assumption. However the real problem is higher up where the code makes assumptions and even declarations to itself that are not true.
image

The code is assuming and declaring that $form_object is a \Drupal\media\MediaForm which is not always the case.
It is also assuming and declaring that $entity \Drupal\media\MediaInterface which is not always the case.

@swirtSJW
Copy link
Contributor Author

swirtSJW commented Jan 12, 2024

Trying to get the definition of the field fails, because the call to
$fieldDefinition = $entity->getFieldDefinition('image'); is giving it the wrong information. The method getFieldDefinition is expecting the name of the field, not the type of the field. image is a type, not the field name.
image

Here is the difference between giving it the field type and the field name.
image

Suggest grabbing the field name dynamically rather than hardcoding.
$entity->getFieldDefinition($element['#field_name']);
image

The field name of "image" only exists on the "Media" entity. All other image fields would have some other name that would never be "image" because they follow entity field names that are field instance dependent and prefixed with "field_".

@swirtSJW
Copy link
Contributor Author

Why does this fail on content model documents and no where else? Because content model documents are the only entity that has an image field. All other places in the site where images are use an entity reference to media, and media is the only content type where this new code works. Adding an image field to any content type directly, will also throw a fatal error.

Example: Say we wanted to have an image field on events for a map, but we did not want to have the map be reuseable and end up in the media library. I added an image field_map to events and here is the result when trying to create an event
image

@swirtSJW
Copy link
Contributor Author

swirtSJW commented Jan 12, 2024

Summary big picture issue: The current code in imageFieldWidgetProcess() is hunting for an image field on every single entity that that Drupal loads. However, inside that function, the assumption is made that it is only ever operating on a "media" entity and this assumption runs into trouble.

There are two possible solutions depending on the intention of code.

  1. If the intent of the code is to alter all image type fields everywhere on all entities (node, term, paragraph, media, menu, and content model doc) to add the alt modifications then the internal code needs to not assume it is operating on a media entity. It needs to be more generic. I believe this is the intention, so this is the approach I took with this PR VACMS-16827 Fix loading of alt text field definitions. #16832
  2. If the intent of the code is to only alter the image field on media entities and no-where else, then the code needs to be properly scoped to not hunt for image fields on anything other than media entities.

@jilladams jilladams added Drupal engineering CMS team practice area CMS Team CMS Product team that manages both editor exp and devops labels Jan 12, 2024
@jilladams
Copy link
Contributor

Noting: checked in with Marcia and Erika on this re: whether Facilities should go ahead and put up a PR for the code change Steve identified, or would like to own the PR. Marcia ok'ed putting up a PR. I've moved the associated PR from draft, and tagged in code reviewers. Not sure if this'll move today as I know a couple of people are out on wellness / before the 3-day weekend, so if not, it'll go after the break.

@jilladams jilladams added Public Websites Scrum team in the Sitewide crew Facilities Facilities products (VAMC, Vet Center, etc) and removed Public Websites Scrum team in the Sitewide crew Needs refining Issue status labels Jan 16, 2024
@jilladams
Copy link
Contributor

(Updating assignment / sprint to reflect PR owner.)

@swirtSJW
Copy link
Contributor Author

PR was just approved by CMS team and I merged it. This could be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CMS Team CMS Product team that manages both editor exp and devops Defect Something isn't working (issue type) Drupal engineering CMS team practice area Facilities Facilities products (VAMC, Vet Center, etc) sitewide
Projects
None yet
Development

No branches or pull requests

2 participants