You are here

public function PurgeBlock::build in Purge 8.3

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

modules/purge_ui/src/Plugin/Block/PurgeBlock.php, line 55

Class

PurgeBlock
Let site administrators purge the current page.

Namespace

Drupal\purge_ui\Plugin\Block

Code

public function build() {

  // Assure that purge_block_id exists so that we can render a unique form.
  $config = $this
    ->getConfiguration();
  if (!isset($config['purge_block_id']) || empty($config['purge_block_id'])) {
    return [
      '#cache' => [
        'max-age' => 0,
      ],
      '#markup' => $this
        ->t('Config not found, please reconfigure block!'),
    ];
  }

  // Directly instantiate the form, to inject the configuration to its
  // constructor. Normally, instantiating with getForm() would pass in the
  // parameters only to FormBase::buildForm(), which is sadly too late as we
  // need the unique form ID already in FormBase::getFormId().
  // See https://www.drupal.org/node/2188851 for more information.
  $form = PurgeBlockForm::create($this->container, $config);
  return $this->container
    ->get('form_builder')
    ->getForm($form);
}