You are here

public function SimplenewsSubscriptionBlock::build in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Block/SimplenewsSubscriptionBlock.php \Drupal\simplenews\Plugin\Block\SimplenewsSubscriptionBlock::build()
  2. 3.x src/Plugin/Block/SimplenewsSubscriptionBlock.php \Drupal\simplenews\Plugin\Block\SimplenewsSubscriptionBlock::build()

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/SimplenewsSubscriptionBlock.php, line 161

Class

SimplenewsSubscriptionBlock
Provides an 'Simplenews subscription' block with all available newsletters and an email field.

Namespace

Drupal\simplenews\Plugin\Block

Code

public function build() {

  /** @var \Drupal\simplenews\Form\SubscriptionsBlockForm $form_object */
  $form_object = $this->entityTypeManager
    ->getFormObject('simplenews_subscriber', 'block');
  $form_object
    ->setUniqueId($this->configuration['unique_id']);
  $form_object
    ->setNewsletterIds($this->configuration['newsletters']);
  $form_object->message = $this->configuration['message'];

  // Set the entity on the form.
  if ($user = \Drupal::currentUser()) {
    if ($subscriber = simplenews_subscriber_load_by_uid($user
      ->id())) {
      $form_object
        ->setEntity($subscriber);
    }
    else {
      $form_object
        ->setEntity(Subscriber::create()
        ->fillFromAccount($user));
    }
  }
  else {
    $form_object
      ->setEntity(Subscriber::create());
  }
  return $this->formBuilder
    ->getForm($form_object);
}