0

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 ;)

0

I'm not sure exactly what's going on here, but looking at the documentation for hook_view_mode_alter, it looks like you're not resetting the view mode properly. It looks like you need to set it like:

/**
 * 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' ) {
    $view_mode = $entity->change_view->value;
  }
}
  • Thanks for looking at this with me. Even if I remove the intermediary assignment to $change_to_view_mode, I get the same WSOD with the same issue . I do see that $view_mode is now set to standard prior to the drupal_set_messages. Which is fine, I was just adding the intermediate variable to see if my code was executing. However, there is still something hinky going on which presents itself in the error log as if there is an array/single value issue but I'm still thinking its a cache issue somehow. – J W Sep 29 '18 at 3:29
  • I reckon that error is caused by your code you use to create the field and not the code you posted above. My best guess is that Drupal don't know which formatter it can use for your custom field (change_view). If you comment this entire hook, you still get the error, right? – sonfd Sep 29 '18 at 13:37
  • To clarify a little, you were setting $entity->view_mode and then saving the entity rather than just setting the $view_mode var that this hook uses. I'm not sure if you've updated that after reading my answer, but that's just wrong, you shouldn't change the entity and save it here. – sonfd Sep 29 '18 at 13:50
  • I did make an update after reading your answer. Removing the entity save corrected this issue. I was able to get the correct view mode using your code above. Thanks again for taking time out of your day to help. – J W Sep 29 '18 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.