Reassign $view_mode using entity field passed to hook_entity_view_mode_alter()

I’m using baseFieldDefinitions to create my custom entity form. With gracious help here, I’ve been able to successfully generate a radio button on the form which lets the user select what view mode to use for the display.

Goal: Get the selected value from the radio button on the form and use it to reset the view mode for the display.

Problem: Other than my general confusion, I believe I have a cache issue when invoking the hook to alter the view mode. I’d like to think I was finally able to overcome the array value to single value assignment problems.

I added this code into MY-MODULE.module:

/**  * Implements hook_entity_view_mode_alter().  */ function nfl_entity_view_mode_alter(&$view_mode, DrupalCoreEntityEntityInterface $entity, $context) {     // Change the view mode to the value selected via radio button on the form.      if ($entity->getEntityTypeId() == 'nfl_fact'  ) {  /*<-- restrict to custom entity */          $change_to_view_mode = $entity->change_view->value;  /*<-- get the lowercase value -- not Initial Cap Label  */          drupal_set_message(sprintf('<pre> change_to_view_mode %s</pre>', print_r($change_to_view_mode, TRUE)));          drupal_set_message(sprintf('<pre> view_mode before %s</pre>', print_r($view_mode, TRUE)));           //  $view_mode = $change_to_view_mode;  /*<--- reassign view mode here -didn't work*/         $entity->view_mode[LANGUAGE_NONE][0]['value'] = $val[0]->change_to_view_mode;  /* <-- reassign */         $entity->save();          drupal_set_message(sprintf('<pre> view_mode after reassignment %s</pre>', print_r($view_mode, TRUE)));      } } 

I added some drupal_set_message entries to reassure myself that I was getting the variable values changed.

<pre> change_to_view_mode standard</pre> <pre> view_mode before full</pre> <pre> view_mode after reassignment standard</pre> Cache cleared. 

When I click on my link from the viewlist builder, I don’t get the display. I get White Screen of Death with the error in the PHP log:

(first few lines)

[29-Sep-2018 02:15:53 UTC] TypeError: Argument 1 passed to DrupalCoreFormOptGroup::flattenOptions() must be of the type array, null given,     called in C:wampwwwdrupal8webcoremodulesoptionssrcPluginFieldFieldFormatterOptionsDefaultFormatter.php on line 40     in C:wampwwwdrupal8webcorelibDrupalCoreFormOptGroup.php on line 23 #0 C:wampwwwdrupal8webcoremodulesoptionssrcPluginFieldFieldFormatterOptionsDefaultFormatter.php(40): DrupalCoreFormOptGroup::flattenOptions(NULL) #1 C:wampwwwdrupal8webcorelibDrupalCoreFieldFormatterBase.php(80): DrupaloptionsPluginFieldFieldFormatterOptionsDefaultFormatter->v 

I’m confused. Also am open to guidance on how to better format this question if it looks too goofy 😉

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Reassign $view_mode using entity field passed to hook_entity_view_mode_alter()

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.