Hi there,

Thanks for the Name module! I ran into a pretty obscure issue with Views, but I think it might point to a larger issue. In the name field formatter plugin, getLinkableTargets() calls $this->fieldDefinition->getTargetBundle(). This is all well and good for the content display configuration screen. In that circumstance $this->fieldDefinition seems to be a Drupal\field\Entity\FieldConfig. When adding a name field to Views and the format configuration form is requested, $this->fieldDefinition is a Drupal\Core\Field\BaseFieldDefinition. In the latter case, getTargetBundle() seems to always return NULL. Most of the time this doesn't seem to matter. Many bundle field definitions don't seem to care about receiving a NULL bundle. However Commerce Order Items for instance, throw if the bundle is missing. This means you can't add a name field that is attached to an Order Item to a field based View. It produces an error as such:

ResponseText: The website encountered an unexpected error. Please try again later.RuntimeException: Could not load the "" order item type. in Drupal\commerce_order\Entity\OrderItem::bundleFieldDefinitions() (line 460 of modules/contrib/commerce/modules/order/src/Entity/OrderItem.php). Drupal\Core\Entity\EntityFieldManager->buildBundleFieldDefinitions('commerce_order_item', NULL, Array) (Line: 338)
Drupal\Core\Entity\EntityFieldManager->getFieldDefinitions('commerce_order_item', NULL) (Line: 314)
Drupal\name\Plugin\Field\FieldFormatter\NameFormatter->getLinkableTargets() (Line: 196)
Drupal\name\Plugin\Field\FieldFormatter\NameFormatter->settingsForm(Array, Object) (Line: 519)
Drupal\views\Plugin\views\field\EntityField->buildOptionsForm(Array, Object) (Line: 167)
Drupal\views_ui\Form\Ajax\ConfigHandler->buildForm(Array, Object)
call_user_func_array(Array, Array) (Line: 519)
Drupal\Core\Form\FormBuilder->retrieveForm('views_ui_config_item_form', Object) (Line: 276)
Drupal\Core\Form\FormBuilder->buildForm('views_ui_config_item_form', Object) (Line: 214)
Drupal\views_ui\Form\Ajax\ViewsFormBase->Drupal\views_ui\Form\Ajax\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 216)
Drupal\views_ui\Form\Ajax\ViewsFormBase->ajaxFormWrapper('Drupal\views_ui\Form\Ajax\ConfigHandler', Object) (Line: 149)
Drupal\views_ui\Form\Ajax\ViewsFormBase->getForm(Object, 'page_1', 'ajax') (Line: 36)
Drupal\views_ui\Form\Ajax\AddHandler->getForm(Object, 'page_1', 'ajax', 'field')
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 151)
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: 50)
Drupal\ban\BanMiddleware->handle(Object, 1, 1) (Line: 84)
Drupal\shield\ShieldMiddleware->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)

From my perspective, it seems as though $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle); should never receive a NULL bundle. However, I'm not quite sure why the field definition class changes between the entity display configuration page and Views. Furthermore in the views context, since we are dealing with potentially numerous bundles, I'm not sure we can ask for just one bundle's field definitions (perhaps this answers the question of field definition classes).

We can create linked names via rewriting with replacement tokens in Views. I'm not sure how to handle the additional preferred settings, which are also reliant on a specific bundle. Perhaps if these functionalities are desired, the view should be showing rendered entities rather than fields. I'm going to create a patch that does not present the link options or additional preferred settings in a field based Views context. I'm happy to help work on a patch going in a different direction, I'm just not sure what that would be. I could also be completely off base. Thanks.

Comments

lkacenja created an issue. See original summary.

lkacenja’s picture

Issue summary: View changes
lkacenja’s picture

Here is the patch that hides bundle-reliant configuration, when a bundle is not available.

lkacenja’s picture

Title: Name fields on Commerce Order Items can't be used in Views » Name field does not pass bundle to getFieldDefinitions() in Views
lunk rat’s picture

Status: Active » Reviewed & tested by the community

I had the same problem using Name field on a Commerce order item type. Tried to add the Name field to the cart form view, got AJAX errors when Views UI tried to load the field configuration modal.

Patch in #3 applies cleanly to 8.x-1.0-rc2 and indeed fixes the issue. Marking RTBC now.

Thank you @lkacenja for the detailed report and patch!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 3: name-fields-commerce-order-items-in-views-3079476-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

nkoporec’s picture

Fixed the phpcs issues, let's see if the test are passing before merging.

  • nkoporec committed d492ae1 on 8.x-1.x
    Issue #3079476 by lkacenja, nkoporec: Name field does not pass bundle to...
nkoporec’s picture

Status: Needs work » Fixed

commited!

Status: Fixed » Closed (fixed)

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