BlockContentBlock.php 6.57 KB
Newer Older
1 2
<?php

3
namespace Drupal\block_content\Plugin\Block;
4

5
use Drupal\block_content\BlockContentUuidLookup;
6
use Drupal\Core\Access\AccessResult;
7 8
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockManagerInterface;
9 10
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
11
use Drupal\Core\Form\FormStateInterface;
12
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
13
use Drupal\Core\Routing\UrlGeneratorInterface;
14
use Drupal\Core\Session\AccountInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16 17 18 19

/**
 * Defines a generic custom block type.
 *
20
 * @Block(
21
 *  id = "block_content",
22
 *  admin_label = @Translation("Custom block"),
23
 *  category = @Translation("Custom"),
24
 *  deriver = "Drupal\block_content\Plugin\Derivative\BlockContent"
25 26
 * )
 */
27
class BlockContentBlock extends BlockBase implements ContainerFactoryPluginInterface {
28 29 30 31

  /**
   * The Plugin Block Manager.
   *
32
   * @var \Drupal\Core\Block\BlockManagerInterface
33 34 35
   */
  protected $blockManager;

36
  /**
37
   * The entity type manager service.
38
   *
39
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
40
   */
41
  protected $entityTypeManager;
42

43 44 45
  /**
   * The Drupal account to use for checking for access to block.
   *
46
   * @var \Drupal\Core\Session\AccountInterface
47 48 49
   */
  protected $account;

50 51 52 53 54 55 56
  /**
   * The block content entity.
   *
   * @var \Drupal\block_content\BlockContentInterface
   */
  protected $blockContent;

57 58 59 60 61 62 63
  /**
   * The URL generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

64 65 66 67 68 69 70 71 72 73 74 75 76 77
  /**
   * The block content UUID lookup service.
   *
   * @var \Drupal\block_content\BlockContentUuidLookup
   */
  protected $uuidLookup;

  /**
   * The entity display repository.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
   */
  protected $entityDisplayRepository;

78
  /**
79
   * Constructs a new BlockContentBlock.
80 81 82 83 84
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
85
   * @param mixed $plugin_definition
86
   *   The plugin implementation definition.
87
   * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
88
   *   The Plugin Block Manager.
89 90
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
91 92
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account for which view access should be checked.
93 94
   * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
   *   The URL generator.
95 96 97 98
   * @param \Drupal\block_content\BlockContentUuidLookup $uuid_lookup
   *   The block content UUID lookup service.
   * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
   *   The entity display repository.
99
   */
100
  public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockManagerInterface $block_manager, EntityTypeManagerInterface $entity_type_manager, AccountInterface $account, UrlGeneratorInterface $url_generator, BlockContentUuidLookup $uuid_lookup, EntityDisplayRepositoryInterface $entity_display_repository) {
101 102 103
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->blockManager = $block_manager;
104
    $this->entityTypeManager = $entity_type_manager;
105
    $this->account = $account;
106
    $this->urlGenerator = $url_generator;
107 108
    $this->uuidLookup = $uuid_lookup;
    $this->entityDisplayRepository = $entity_display_repository;
109 110 111 112 113
  }

  /**
   * {@inheritdoc}
   */
114
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
115 116 117 118 119
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('plugin.manager.block'),
120
      $container->get('entity_type.manager'),
121
      $container->get('current_user'),
122 123 124
      $container->get('url_generator'),
      $container->get('block_content.uuid_lookup'),
      $container->get('entity_display.repository')
125 126
    );
  }
127 128

  /**
129
   * {@inheritdoc}
130
   */
131
  public function defaultConfiguration() {
132
    return [
133 134 135
      'status' => TRUE,
      'info' => '',
      'view_mode' => 'full',
136
    ];
137 138 139
  }

  /**
140
   * Overrides \Drupal\Core\Block\BlockBase::blockForm().
141 142 143
   *
   * Adds body and description fields to the block configuration form.
   */
144
  public function blockForm($form, FormStateInterface $form_state) {
145 146 147 148 149
    $block = $this->getEntity();
    if (!$block) {
      return $form;
    }
    $options = $this->entityDisplayRepository->getViewModeOptionsByBundle('block_content', $block->bundle());
150

151
    $form['view_mode'] = [
152
      '#type' => 'select',
153
      '#options' => $options,
154 155
      '#title' => $this->t('View mode'),
      '#description' => $this->t('Output the block in this view mode.'),
156 157
      '#default_value' => $this->configuration['view_mode'],
      '#access' => (count($options) > 1),
158
    ];
159
    $form['title']['#description'] = $this->t('The title of the block as shown to the user.');
160 161 162 163
    return $form;
  }

  /**
164
   * {@inheritdoc}
165
   */
166
  public function blockSubmit($form, FormStateInterface $form_state) {
167
    // Invalidate the block cache to update custom block-based derivatives.
168 169
    $this->configuration['view_mode'] = $form_state->getValue('view_mode');
    $this->blockManager->clearCachedDefinitions();
170 171
  }

172 173 174 175 176 177 178 179 180 181
  /**
   * {@inheritdoc}
   */
  protected function blockAccess(AccountInterface $account) {
    if ($this->getEntity()) {
      return $this->getEntity()->access('view', $account, TRUE);
    }
    return AccessResult::forbidden();
  }

182
  /**
183
   * {@inheritdoc}
184
   */
185
  public function build() {
186
    if ($block = $this->getEntity()) {
187
      return $this->entityTypeManager->getViewBuilder($block->getEntityTypeId())->view($block, $this->configuration['view_mode']);
188 189
    }
    else {
190 191
      return [
        '#markup' => $this->t('Block with uuid %uuid does not exist. <a href=":url">Add custom block</a>.', [
192
          '%uuid' => $this->getDerivativeId(),
193
          ':url' => $this->urlGenerator->generate('block_content.add_page'),
194
        ]),
195
        '#access' => $this->account->hasPermission('administer blocks'),
196
      ];
197 198
    }
  }
199 200 201 202 203 204 205 206 207

  /**
   * Loads the block content entity of the block.
   *
   * @return \Drupal\block_content\BlockContentInterface|null
   *   The block content entity.
   */
  protected function getEntity() {
    if (!isset($this->blockContent)) {
208
      $uuid = $this->getDerivativeId();
209 210 211
      if ($id = $this->uuidLookup->get($uuid)) {
        $this->blockContent = $this->entityTypeManager->getStorage('block_content')->load($id);
      }
212 213 214 215
    }
    return $this->blockContent;
  }

216
}