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 😉
Sponsored by SupremePR