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, Drupal\Core\Entity\EntityInterface $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 Drupal\Core\Form\OptGroup::flattenOptions() must be of the type array, null given,
called in C:\wamp\www\drupal8\web\core\modules\options\src\Plugin\Field\FieldFormatter\OptionsDefaultFormatter.php on line 40
in C:\wamp\www\drupal8\web\core\lib\Drupal\Core\Form\OptGroup.php on line 23
#0 C:\wamp\www\drupal8\web\core\modules\options\src\Plugin\Field\FieldFormatter\OptionsDefaultFormatter.php(40): Drupal\Core\Form\OptGroup::flattenOptions(NULL)
#1 C:\wamp\www\drupal8\web\core\lib\Drupal\Core\Field\FormatterBase.php(80): Drupal\options\Plugin\Field\FieldFormatter\OptionsDefaultFormatter->v
I'm confused. Also am open to guidance on how to better format this question if it looks too goofy ;)