When loading a view page that shows lists of teasers some containing premium content, others not, the following warning is shown to the screen.

Note: $config['system.logging']['error_level'] = 'verbose'; is set in settings.php for local development.

Notice: Undefined offset: 0 in nopremium_entity_view_mode_alter() (line 106 of modules/contrib/nopremium/nopremium.module).
nopremium_entity_view_mode_alter('teaser', Object, Array) (Line: 539)
Drupal\Core\Extension\ModuleHandler->alter('entity_view_mode', 'teaser', Object, Array) (Line: 161)
Drupal\Core\Entity\EntityViewBuilder->getBuildDefaults(Object, 'teaser') (Line: 59)
Drupal\node\NodeViewBuilder->getBuildDefaults(Object, 'teaser') (Line: 138)
Drupal\Core\Entity\EntityViewBuilder->viewMultiple(Array, 'teaser', 'en') (Line: 111)
Drupal\Core\Entity\EntityViewBuilder->view(Object, 'teaser', 'en') (Line: 90)
Drupal\views\Entity\Render\TranslationLanguageRenderer->preRender(Array) (Line: 178)
Drupal\views\Plugin\views\row\EntityRow->preRender(Array) (Line: 434)
Drupal\views\Plugin\views\style\StylePluginBase->preRender(Array) (Line: 1505)
Drupal\views\ViewExecutable->render() (Line: 183)
Drupal\views\Plugin\views\display\Page->execute() (Line: 1627)
Drupal\views\ViewExecutable->executeDisplay('page_1', Array) (Line: 77)
Drupal\views\Element\View::preRenderViewElement(Array)
call_user_func(Array, Array) (Line: 378)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156)
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: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 664)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Similarly, on the full node display of a node type with premium node option enabled, but the node was created before the module was installed (thus premium is unchecked, and the node has not been edited) I get a similar error:

nopremium_entity_view_mode_alter('full', Object, Array) (Line: 539)
Drupal\Core\Extension\ModuleHandler->alter('entity_view_mode', 'full', Object, Array) (Line: 161)
Drupal\Core\Entity\EntityViewBuilder->getBuildDefaults(Object, 'full') (Line: 59)
Drupal\node\NodeViewBuilder->getBuildDefaults(Object, 'full') (Line: 138)
Drupal\Core\Entity\EntityViewBuilder->viewMultiple(Array, 'full', NULL) (Line: 111)
Drupal\Core\Entity\EntityViewBuilder->view(Object, 'full') (Line: 96)
Drupal\Core\Entity\Controller\EntityViewController->view(Object, 'full', NULL) (Line: 55)
Drupal\node\Controller\NodeViewController->view(Object, 'full', NULL)
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: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 664)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

If you edit then re-save the nodes that were created before installing the module, the error messages go away.

Comments

jwilson3 created an issue. See original summary.

jwilson3’s picture

Issue summary: View changes
jwilson3’s picture

Issue summary: View changes
saurabh.dhariwal’s picture

Status: Active » Needs review
StatusFileSize
new1.46 KB

@jwilson3, I got the error which you mentioned above, Please review my attached patch for the same.

megachriz’s picture

This is almost the same fix, but with $entity->get('premium')->getValue()[0]['value'] shortened to $entity->premium->value. This makes the code more readable.

Also includes a test to demonstrate the issue. It would be great if #3066059: Enable automated testing for project gets done, then the testbot would be able to run the test.

megachriz’s picture

Oops, I diffed the patches at the wrong branch locally. Working on too many patches ;).

megachriz’s picture

Reroll.

  • MegaChriz authored 2888222 on 8.x-1.x
    Issue #2958213 by MegaChriz, saurabh.dhariwal, jwilson3: Undefined...
megachriz’s picture

Status: Needs review » Fixed

Committed #7.

Status: Fixed » Closed (fixed)

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