You are here

SimplenewsContentSelectionBlock.php in Simplenews Content Selection 8

File

src/Plugin/Block/SimplenewsContentSelectionBlock.php
View source
<?php

namespace Drupal\simplenews_content_selection\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Provides a 'Simplenews Content Selection' block.
 *
 * @Block(
 *   id = "simplenews_content_selection_block",
 *   admin_label = @Translation("Simplenews Content Selection Block")
 * )
 */
class SimplenewsContentSelectionBlock extends BlockBase {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   *
   * This method sets the block default configuration. This configuration
   * determines the block's behavior when a block is initially placed in a
   * region. Default values for the block configuration form should be added to
   * the configuration array. System default configurations are assembled in
   * BlockBase::__construct() e.g. cache setting and block title visibility.
   *
   * @see \Drupal\block\BlockBase::__construct()
   */
  public function defaultConfiguration() {
    return [
      'simplenews_content_selection_block' => $this
        ->t('A default value. This block was created at %time', [
        '%time' => date('c'),
      ]),
    ];
  }

  /**
   * {@inheritdoc}
   *
   * This method defines form elements for custom block configuration. Standard
   * block configuration fields are added by BlockBase::buildConfigurationForm()
   * (block title and title visibility) and BlockFormController::form() (block
   * visibility settings).
   *
   * @see \Drupal\block\BlockBase::buildConfigurationForm()
   * @see \Drupal\block\BlockFormController::form()
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form['simplenews_content_selection_block'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Block contents'),
      '#description' => $this
        ->t('This text will appear in the example block.'),
      '#default_value' => $this->configuration['simplenews_content_selection_block'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   *
   * This method processes the blockForm() form fields when the block
   * configuration form is submitted.
   *
   * The blockValidate() method can be used to validate the form submission.
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['simplenews_content_selection_block'] = $form_state
      ->getValue('simplenews_content_selection_block');
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    return [
      '#markup' => $this->configuration['simplenews_content_selection_block'],
    ];
  }

}

Classes

Namesort descending Description
SimplenewsContentSelectionBlock Provides a 'Simplenews Content Selection' block.