1

On our D8 site we have a content type with a webform reference field field_reference_webform. We then created a view with a custom views display style plugin for above content type that uses the webform reference field field_reference_webform.

What we want to achieve is to print the webform field in our custom view display style directly via views-view-our-display-plugin.html.twig.

The contents of our Webform reference field, however, wouldn't print. We tried to print it with below code:

{% if rows|length %}
{% for row in rows %}
{{ row._entity.field_reference_webform }}
{% endfor %}
{% endif %}

Instead we get the following error:

Object of type Drupal\webform\Plugin\Field\FieldType\WebformEntityReferenceFieldItemList cannot be printed

We cannot figure out what we got wrong.

We are on Drupal 8.6.2 using Webform 8.x-5.0-rc22

2

At first glance it would seem you need to render this webform yourself in a preprocess function and then pass it to twig. You are trying to access the entity object using row._entity.field_reference_webform which is protected from outside access. However views should already be rendering this webform. I think maybe your template file is just setup wrong. All you have done is create a view, and put a template file for it in your theme correct?

For row level output, if your display is setup as unformatted list, your views twig file should be named:

views-view-unformatted--our-display-plugin.html.twig

and the basic code inside it should be:

{% if title %}
  <h3>{{ title }}</h3>
{% endif %}
{% for row in rows %}
  {%
    set row_classes = [
      default_row_class ? 'views-row',
    ]
  %}
  <div{{ row.attributes.addClass(row_classes) }}>
    {{- row.content -}}
  </div>
{% endfor %}

Just use {{ row.content }} which should have the rendered webform in it already.

If that doesn't work perhaps we need more information on what exactly youre trying to create a template file for. The view container, the row level, or the field level.

  • Thanks Matt. We elaborated on your answer and were able to solve it with your answer. I have posted the full implementation below. – criscom Oct 24 '18 at 8:42
1

Thanks to Matt's answer we could solve the problem as follows:

In custom_view_format.theme.inc we implemented a preprocess function:

function template_preprocess_views_view_sunlime_custom_view_format(&$variables) {
// View options set by user.
$options = $variables['view']->style_plugin->options;

$rows = $variables['rows'];

if($rows && sizeof($rows) > 0){
  $idx = 0;
  foreach($rows as $row){

  // We need to preprocess the referenced webform object to output it as desired in the twig template
  $webform = $row->_entity->field_candidate_status_client[0];

  // Get the machine name from the referenced webform data
  $webform_machinename = $webform->target_id;

  // Init the webform object with the given machine name
  $my_form = \Drupal\webform\Entity\Webform::load($webform_machinename);


  // Generate the output from the generated webform object

  $output = \Drupal::entityTypeManager()
                   ->getViewBuilder('webform')
                   ->view($my_form);

  // Set this output into a custom variable so it can be accessed in the twig file
  $variables['sl_webform'][$idx]['field_candidate_status_client'] = $output;

  $idx++;
  }
 }
}

Then he printed the webform field in the twig template like below:

<div class="col col-lg-6">
  {# Access the rendered webform from our own variables array - see docroot/modules/custom/custom_view_format/sunlime_custom_view_format.theme.inc #}
  {{ attribute(sl_webform,idx).field_candidate_status_client }}
</div>

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.