After checking out the 8.x-2.x branch on a site that previously had 8.x-2.0-alpha3 installed, I got the following error when running drush cr, or visiting a page:

Error: Call to a member function getTargetEntityTypeId() on null in /var/www/drupalvm/drupal/web/modules/contrib/views_natural_sort/src/ViewsNaturalSortService.php on line 244

Stack trace:
#0 /var/www/drupalvm/drupal/web/modules/contrib/views_natural_sort/src/ViewsNaturalSortService.php(128): Drupal\views_natural_sort\ViewsNaturalSortService->getViewsBaseTable(NULL)
#1 /var/www/drupalvm/drupal/web/modules/contrib/views_natural_sort/views_natural_sort.views.inc(8): Drupal\views_natural_sort\ViewsNaturalSortService->getSupportedEntityProperties()
#2 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(501): views_natural_sort_views_data_alter(Array, NULL, NULL)
#3 /var/www/drupalvm/drupal/web/core/modules/views/src/ViewsData.php(254): Drupal\Core\Extension\ModuleHandler->alter('views_data', Array)
#4 /var/www/drupalvm/drupal/web/core/modules/views/src/ViewsData.php(162): Drupal\views\ViewsData->getData()
#5 /var/www/drupalvm/drupal/web/core/modules/views/src/Plugin/Derivative/ViewsEnti in /var/www/drupalvm/drupal/web/modules/contrib/views_natural_sort/src/ViewsNaturalSortService.php, line 244

Checking out the 8.x-2.0-alpha3 tag again, the issue did not recur.

Comments

malcomio created an issue. See original summary.

malcomio’s picture

Status: Active » Needs review
StatusFileSize
new3.13 KB

Adding a check to see if $fieldDefinition is not empty seems to address the issue - still need to do more testing to see if this change has any side-effects.

generalredneck’s picture

Yeah my first question would be why was it NULL in the first place. I'll see if I can reproduce on a new site install... with views_natural_sort_test installed. If not we need to see what field type causes that.

It's very possible that I'm in the middle of somethijng there too like trying to get text fields to work :p

Been a while so I can't say for sure

asak’s picture

I hit this error as well. The patch resolved the error, and I can see that the problematic field was from the video entity: Notice: Undefined index: field_media_oembed_video in Drupal\views_natural_sort\ViewsNaturalSortService->getSupportedEntityProperties() (line 124 of modules/contrib/views_natural_sort/src/ViewsNaturalSortService.php).

generalredneck’s picture

I'll give that a shot.

imclean’s picture

In support of #4, I did some debugging and noticed $fieldConfig is null for the field field_media_oembed_video.

The problem is the field definitions for the entity type doesn't include that field. The field is attached to the remote_video bundle of the media entity type.

The field definitions are obtained here:

$fieldConfigs = $this->entityFieldManager->getBaseFieldDefinitions($entity_type , reset($field_info['bundles']));

See getBaseFieldDefinitions() documentation:

Gets the base field definitions for a content entity type.

Only fields that are not specific to a given bundle or set of bundles are returned. This excludes configurable fields, as they are always attached to a specific bundle.

While the field field_media_oembed_video is specific to the bundle, it may be considered a "configurable field", but I'm not sure. It can't be deleted so I'm not sure if that means it is configurable.

sker101’s picture

StatusFileSize
new1.4 KB

Hi, I'm seeing the other error now after applying the patch.

Notice: Undefined index: field_custom in Drupal\views_natural_sort\ViewsNaturalSortService->getSupportedEntityProperties() 

It's one of my custom field from my node entity and it's happening whenever I try to save the one of the views or try to install site from config on our CI environment.

After looking into the codebase, what I don't get is it's trying to grab the field config of all the entity fields from the values returned by the method "getBaseFieldDefinitions" and which won't return the field config for custom fields like what comment #6 pointed out.

I created a patch that replace the method "getBaseFieldDefinitions" with "getFieldDefinitions" because the method "getFieldDefinitions" would return config for all fields including the custom fields. Also, the original method "getBaseFieldDefinitions" would only take one argument but it's currently passing two arguments, so I assumed "getFieldDefinitions" is the one that should be called here.

I also updated the logic a little bit based on the first patch, so it looks clearer in the patch file. The previous logic was just wrapping all the code inside an if statement but it created a lot of file diffs.

Status: Needs review » Needs work

The last submitted patch, 7: fatal-2931206-7.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

sker101’s picture

StatusFileSize
new1.62 KB

Okay, it looks like we still need to use the "getBaseFieldDefinitions" method for entity types that don't have any bundles, for example, comment.

Here's the updated patch.

sker101’s picture

Status: Needs work » Needs review

generalredneck’s picture

Status: Needs review » Fixed

This worked out with my testing as well. making this as fix for the next release.

Status: Fixed » Closed (fixed)

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