class CustomBlockBlock

Defines a generic custom block type.

Plugin annotation


@Plugin(
 id = "custom_block",
 admin_label = @Translation("Custom block"),
 module = "custom_block",
 derivative = "Drupal\custom_block\Plugin\Derivative\CustomBlock"
)

Hierarchy

Expanded class hierarchy of CustomBlockBlock

File

drupal/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Block/CustomBlockBlock.php, line 24
Contains \Drupal\custom_block\Plugin\Block\CustomBlockBlock.

Namespace

Drupal\custom_block\Plugin\Block
View source
class CustomBlockBlock extends BlockBase {

  /**
   * Overrides \Drupal\block\BlockBase::settings().
   */
  public function settings() {
    return array(
      'status' => TRUE,
      'info' => '',
      'view_mode' => 'full',
    );
  }

  /**
   * Overrides \Drupal\block\BlockBase::blockForm().
   *
   * Adds body and description fields to the block configuration form.
   */
  public function blockForm($form, &$form_state) {
    $options = array();
    $view_modes = entity_get_view_modes('custom_block');
    foreach ($view_modes as $view_mode => $detail) {
      $options[$view_mode] = $detail['label'];
    }
    $form['custom_block']['view_mode'] = array(
      '#type' => 'select',
      '#options' => $options,
      '#title' => t('View mode'),
      '#description' => t('Output the block in this view mode.'),
      '#default_value' => $this->configuration['view_mode'],
    );
    $form['title']['#description'] = t('The title of the block as shown to the user.');
    return $form;
  }

  /**
   * Overrides \Drupal\block\BlockBase::blockSubmit().
   */
  public function blockSubmit($form, &$form_state) {

    // Invalidate the block cache to update custom block-based derivatives.
    if (module_exists('block')) {
      drupal_container()
        ->get('plugin.manager.block')
        ->clearCachedDefinitions();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function build() {

    // @todo Clean up when http://drupal.org/node/1874498 lands.
    list(, $uuid) = explode(':', $this
      ->getPluginId());
    if ($block = entity_load_by_uuid('custom_block', $uuid)) {
      return entity_view($block, $this->configuration['view_mode']);
    }
    else {
      return array(
        '#markup' => t('Block with uuid %uuid does not exist. <a href="!url">Add custom block</a>.', array(
          '%uuid' => $uuid,
          '!url' => url('block/add'),
        )),
        '#access' => user_access('administer blocks'),
      );
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockBase::access public function Indicates whether block-specific criteria allow access to the block. Overrides BlockPluginInterface::access 16
BlockBase::blockValidate public function Adds block type-specific validation for the block form.
BlockBase::form public function Implements \Drupal\block\BlockPluginInterface::form(). Overrides BlockPluginInterface::form 1
BlockBase::getConfig public function Returns the configuration data for the block plugin.
BlockBase::setConfig public function Sets a particular value in the block settings.
BlockBase::submit public function Implements \Drupal\block\BlockPluginInterface::submit(). Overrides BlockPluginInterface::submit
BlockBase::validate public function Implements \Drupal\block\BlockPluginInterface::validate(). Overrides BlockPluginInterface::validate
BlockBase::__construct public function Constructs a Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
CustomBlockBlock::blockForm public function Overrides \Drupal\block\BlockBase::blockForm(). Overrides BlockBase::blockForm
CustomBlockBlock::blockSubmit public function Overrides \Drupal\block\BlockBase::blockSubmit(). Overrides BlockBase::blockSubmit
CustomBlockBlock::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
CustomBlockBlock::settings public function Overrides \Drupal\block\BlockBase::settings(). Overrides BlockBase::settings
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId