You are here

public function SimplenewsSubscriptionBlock::build in Simplenews 8.2

Same name and namespace in other branches
  1. 8 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 162

Class

SimplenewsSubscriptionBlock
Provides a subscription block with all available newsletters and 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.
  $user = \Drupal::currentUser();
  $form_object
    ->setEntity(Subscriber::loadByUid($user
    ->id(), 'create'));
  return $this->formBuilder
    ->getForm($form_object);
}