You are here

public function ContactFormViewBuilder::view in Contact Storage 8

Builds the render array for the provided entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to render.

string $view_mode: (optional) The view mode that should be used to render the entity.

string $langcode: (optional) For which language the entity should be rendered, defaults to the current content language.

Return value

array A render array for the entity.

Throws

\InvalidArgumentException Can be thrown when the set of parameters is inconsistent, like when trying to view a Comment and passing a Node which is not the one the comment belongs to, or not passing one, and having the comment node not be available for loading.

Overrides EntityViewBuilderInterface::view

1 call to ContactFormViewBuilder::view()
ContactFormViewBuilder::viewMultiple in src/ContactFormViewBuilder.php
Builds the render array for the provided entities.

File

src/ContactFormViewBuilder.php, line 86

Class

ContactFormViewBuilder
Provides a contact form view builder.

Namespace

Drupal\contact_storage

Code

public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
  if ($entity
    ->status()) {
    $message = $this->contactMessageStorage
      ->create([
      'contact_form' => $entity
        ->id(),
    ]);
    $form = $this->entityFormBuilder
      ->getForm($message);
    $form['#title'] = $entity
      ->label();
    $form['#cache']['contexts'][] = 'user.permissions';
    $this->renderer
      ->addCacheableDependency($form, $this->config);
  }
  else {

    // Form disabled, display a custom message using a template.
    $form['disabled_form_error'] = [
      '#theme' => 'contact_storage_disabled_form',
      '#contact_form' => $entity,
      '#redirect_uri' => $entity
        ->getThirdPartySetting('contact_storage', 'redirect_uri', ''),
      '#disabled_form_message' => $entity
        ->getThirdPartySetting('contact_storage', 'disabled_form_message', t('This contact form has been disabled.')),
    ];
  }

  // Add required cacheability metadata from the contact form entity, so that
  // changing it invalidates the cache.
  $this->renderer
    ->addCacheableDependency($form, $entity);
  return $form;
}