0

I have panelizer installed so that I can panelize the full content views on a node by node basis on my content types. For example I have a news content type but I have a default panelizer view mode and I have a 'press release' version meant for the news nodes that are considered press releases.

I'm attempting to perform a query on them so I can programmatically set the view modes in bulk instead of having to go through each node and do it manually, but I'm having trouble. The machine names of the views are 'news' and 'press_release' and I'm attempting to change them like so

$node->panelizer->view_mode = 'press_release'; 
$node->save();

But it doesn't seem to have any effect. How would I programmatically change the panelizer view mode on a node?

UPDATE: I used $node->panelizer->getValue(); and this is what I received.

string(4) "news"
array(2) {
  [0]=>
  array(3) {
    ["view_mode"]=>
    string(4) "news"
    ["default"]=>
    string(4) "news"
    ["panels_display"]=>
    array(0) {
    }
  }
  [1]=>
  array(3) {
    ["view_mode"]=>
    string(4) "full"
    ["default"]=>
    string(13) "press_release"
    ["panels_display"]=>
    array(0) {
    }
  }
}

It seemed like instead of getting the view_mode that is set I got every view mode which made me think Iwas on the wrong path.

1 Answer 1

0

It turned out the output of $node->panelizer->getValue(); was giving me a clue that I missed. enter image description here

I had forgotten that there were different panelizer templates for different view modes.

I was able to accomplish what I needed with

    $panelizer_arr = $node->panelizer->getValue();
    for ($i = 0; $i < count($panelizer_arr); $i++) {
        if ($panelizer_arr[$i]['view_mode'] == 'full') {
            $panelizer_arr[$i]['default'] = 'test_display';
        }
    }
    $node->panelizer = $panelizer_arr;
    $node->save();
1
  • Found out the [1] element of the panelizer array didn't always correspond to the 'full' view mode so I fixed it to search for that instead of depending on a number;
    – Amy
    Commented Jan 18, 2018 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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