1

We use Paragraphs quite extensively in our site. We have about 10 bundles available.

What we' like to do is alter how the paragraphs are displayed on various pages; Ie, we'd like to remove bundle a and display only bundle b. Then, in an alternate view, we could have only bundle c and none others displayed.

Short of writing some page/node/paragraphs alters I'm looking for a nice clean way I can achieve this. Maybe 'view modes' for an entire paragraph.

Note paragraphs behave similarly to field collections, so any suggestions for FC are welcome also.

4
  • When you say "on various pages" - what's going to determine how things are displayed? Content type? Context? Something else? Dec 8, 2015 at 13:44
  • Most likely the presence of field content in the content type. But that is the initial use case only. If we can get this working properly we can use contexts as well.
    – Christian
    Dec 8, 2015 at 22:38
  • I think you should clarify this more to make it useful for other visitors. You have paragraphs attached to a node with a paragraph field. And now you want to show only some of them, depending where this node is displayed?
    – donquixote
    Dec 19, 2018 at 10:08
  • One thing you can do is write custom formatters for paragraphs fields.
    – donquixote
    Dec 19, 2018 at 10:08

1 Answer 1

0

So, I fixed this by writing a new module that creates custom panels panes.

Using the base Panels plugin code seen here http://fourword.fourkitchens.com/article/creating-custom-panels-panes-and-use-substitution-too I created a custom panels pane.

With the custom panels pane, I added a setting for bundles;

$bundles = paragraphs_bundle_load();
$options = array();
foreach ($bundles as $key => $data) {
  $options[$key] = $data->name;
}

$form['display_bundles'] = array(
  '#type' => 'checkboxes',
  '#options' => $options,
  '#default_value' => $conf['display_bundles'],
  '#title' => t('What bundles should be displayed?'),
);

So when adding a pane in Panelizer, there is a setting for selecting which paragraph bundles are displayed.

Then, when rendering the pane, I check which bundles are allowed and then render them, returning an html block.

$nid = $args[0];
$node = $contexts['panelizer']->data;

if ($node->type == 'article') {

  $para_ids = $node->field_nova_section_main[LANGUAGE_NONE];
  $para_ids_array = array();
  foreach ($para_ids as $delta => $data) {
    $para_ids_array[] = $data['value'];
  }

  $return = '';
  $paras = paragraphs_item_load_multiple($para_ids_array);
  $idx = 0;
  $keys = array();
  foreach ($paras as $entity_id => $entity_data) {
    if ($conf['display_bundles'][$entity_data->bundle] !== 0) {
      $return .= render(field_view_value('node', $node, 'field_nova_section_main', $node->field_nova_section_main[LANGUAGE_NONE][$idx++]));
    }
    $idx++;
  }
}

$block = new stdClass();
$block->title = '';
$block->content = $return;

return $block;

So now, rather than adding the node field to a Panelized layout, I add my custom pane and select which bundles I display.

Your Answer

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

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