You are here

public function WebformEntityReferenceLinkFormatter::viewElements in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/Field/FieldFormatter/WebformEntityReferenceLinkFormatter.php \Drupal\webform\Plugin\Field\FieldFormatter\WebformEntityReferenceLinkFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/WebformEntityReferenceLinkFormatter.php, line 176

Class

WebformEntityReferenceLinkFormatter
Plugin implementation of the 'Link to webform' formatter.

Namespace

Drupal\webform\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $source_entity = $items
    ->getEntity();
  $this->messageManager
    ->setSourceEntity($source_entity);
  $elements = [];

  /** @var \Drupal\webform\WebformInterface[] $entities */
  $entities = $this
    ->getEntitiesToView($items, $langcode);
  foreach ($entities as $delta => $entity) {

    // Do not display the webform if the current user can't create submissions.
    if ($entity
      ->id() && !$entity
      ->access('submission_create')) {
      continue;
    }
    if ($entity
      ->isOpen()) {
      $link_label = $this
        ->getSetting('label');
      if (strpos($link_label, '[webform_submission') !== FALSE) {
        $link_entity = WebformSubmission::create([
          'webform_id' => $entity
            ->id(),
          'entity_type' => $source_entity
            ->getEntityTypeId(),
          'entity_id' => $source_entity
            ->id(),
        ]);

        // Invoke override settings to all webform handlers to adjust any
        // form settings.
        $link_entity
          ->getWebform()
          ->invokeHandlers('overrideSettings', $link_entity);
      }
      else {
        $link_entity = $entity;
      }
      $link_options = QueryStringWebformSourceEntity::getRouteOptionsQuery($source_entity);
      $link = [
        '#type' => 'link',
        '#title' => [
          '#markup' => $this->tokenManager
            ->replace($link_label, $link_entity),
        ],
        '#url' => $entity
          ->toUrl('canonical', $link_options),
        '#attributes' => $this
          ->getSetting('attributes') ?: [],
      ];
      if ($dialog = $this
        ->getSetting('dialog')) {
        $link['#attributes']['class'][] = 'webform-dialog';
        $link['#attributes']['class'][] = 'webform-dialog-' . $dialog;

        // Attach webform dialog library and options if they are not
        // on every page.
        if (!\Drupal::config('webform.settings')
          ->get('settings.dialog')) {
          $link['#attached']['library'][] = 'webform/webform.dialog';
          $link['#attached']['drupalSettings']['webform']['dialog']['options'] = \Drupal::config('webform.settings')
            ->get('settings.dialog_options');
        }
      }
      $elements[$delta] = $link;
    }
    else {
      $this->messageManager
        ->setWebform($entity);
      $message_type = $entity
        ->isOpening() ? WebformMessageManagerInterface::FORM_OPEN_MESSAGE : WebformMessageManagerInterface::FORM_CLOSE_MESSAGE;
      $elements[$delta] = $this->messageManager
        ->build($message_type);
    }
    $this
      ->setCacheContext($elements[$delta], $entity, $items[$delta]);
  }
  return $elements;
}