You are here

public function WebformSubmissionCompositeField::render in Webform Views Integration 8.5

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides WebformSubmissionField::render

File

src/Plugin/views/field/WebformSubmissionCompositeField.php, line 17

Class

WebformSubmissionCompositeField
Webform submission composite field.

Namespace

Drupal\webform_views\Plugin\views\field

Code

public function render(ResultRow $values) {

  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $this
    ->getEntity($values);
  if ($webform_submission && $webform_submission
    ->access('view')) {
    $webform = $webform_submission
      ->getWebform();

    // Get format and element key.
    $format = $this->options['webform_element_format'];
    $element_key = $this->definition['webform_submission_field'];
    $composite_key = $this->definition['webform_submission_property'];

    // Get element and element handler plugin.
    $element = $webform
      ->getElement($element_key, TRUE);
    if (!$element) {
      return [];
    }

    // Set the format.
    $element['#format'] = $format;
    $composite_element = $element['#webform_composite_elements'][$composite_key];
    $composite_element['#webform_key'] = $element['#webform_key'];
    $options = [
      'composite_key' => $composite_key,
    ];

    // If this is a non-multiple element or a multiple element and we are only
    // showing a specific delta, we can transparently delegate it.
    if (!$this->webformElementManager
      ->getElementInstance($element)
      ->hasMultipleValues($element) || !$this->options['webform_multiple_value']) {
      if (!$this->options['webform_multiple_value']) {
        $options['delta'] = $this->options['webform_multiple_delta'];
      }
      return $this->webformElementManager
        ->invokeMethod('formatHtml', $composite_element, $webform_submission, $options);
    }

    // On the other hand, if we are requested to show all deltas on a multiple
    // element, then we have to manually construct the list.
    $build = [
      '#theme' => 'item_list',
      '#items' => [],
    ];
    $i = 0;
    do {
      $options['delta'] = $i;
      $formatted_item = $this->webformElementManager
        ->invokeMethod('formatHtml', $composite_element, $webform_submission, $options);
      $build['#items'][] = $formatted_item;
      $i++;
    } while ($formatted_item);

    // Strip the last (empty) delta.
    array_pop($build['#items']);
    return $build;
  }
  return [];
}