You are here

public function EntityBlockBlock::build in Entityblock 8

Implements \Drupal\block\BlockBase::blockBuild().

Overrides BlockPluginInterface::build

File

src/Plugin/Block/EntityBlockBlock.php, line 183
Contains \Drupal\entityblock\Plugin\Block\EntityBlockBlock.

Class

EntityBlockBlock
Provides an entityblock block.

Namespace

Drupal\entityblock\Plugin\Block

Code

public function build() {
  $entity = $this
    ->loadEntity();
  $field_value = $this
    ->loadFieldValue();
  if ($entity) {
    if ($field_value) {
      $content = $this->entityTypeManager
        ->getViewBuilder($entity
        ->getEntityTypeId())
        ->view($entity, $field_value->view_mode);
      $content['#title'] = $this
        ->label();
      $content['#entityblock'] = TRUE;
      return $content;
    }
    else {
      return [
        '#markup' => $this
          ->t('EntityBlock has been disabled for this <a href=":url">entity</a>.', [
          ':url' => $entity
            ->toUrl()
            ->toString(),
        ]),
        '#access' => $this->account
          ->hasPermission('administer blocks'),
      ];
    }
  }
  return [
    '#markup' => $this
      ->t('Block with key %key does not exist.', [
      '%key' => $this
        ->getDerivativeId(),
    ]),
    '#access' => $this->account
      ->hasPermission('administer blocks'),
  ];
}