You are here

public function ContactBlock::build in Contact Block 8

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/ContactBlock.php, line 204

Class

ContactBlock
Provides a 'ContactBlock' block.

Namespace

Drupal\contact_block\Plugin\Block

Code

public function build() {
  $form = [];

  /** @var \Drupal\contact\Entity\ContactForm $contact_form */
  $contact_form = $this
    ->getContactForm();
  if ($contact_form) {
    $contact_message = $this
      ->createContactMessage();

    // The personal contact form has a fixed recipient: the user who's
    // contact page we visit. We use the 'user' property from the URL
    // to determine this user. For example: user/{user}.
    if ($contact_message
      ->isPersonal()) {
      $user = $this->routeMatch
        ->getParameter('user');
      $contact_message
        ->set('recipient', $user);
    }
    $form = $this->entityFormBuilder
      ->getForm($contact_message);
    $form['#cache']['contexts'][] = 'user.permissions';
    $this->renderer
      ->addCacheableDependency($form, $contact_form);
    $form['#contextual_links']['contact_block'] = [
      'route_parameters' => [
        'contact_form' => $contact_form
          ->id(),
      ],
    ];
  }
  return $form;
}