class BlockContent in Quick Tabs 8.3
Provides a 'block content' tab type.
Plugin annotation
@TabType(
id = "block_content",
name = @Translation("block"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\quicktabs\TabTypeBase implements TabTypeInterface uses DependencySerializationTrait
- class \Drupal\quicktabs\Plugin\TabType\BlockContent uses StringTranslationTrait
- class \Drupal\quicktabs\TabTypeBase implements TabTypeInterface uses DependencySerializationTrait
Expanded class hierarchy of BlockContent
File
- src/
Plugin/ TabType/ BlockContent.php, line 19
Namespace
Drupal\quicktabs\Plugin\TabTypeView source
class BlockContent extends TabTypeBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function optionsForm(array $tab) {
$plugin_id = $this
->getPluginDefinition()['id'];
$form = [];
$form['bid'] = [
'#type' => 'select',
'#options' => $this
->getBlockOptions(),
'#default_value' => isset($tab['content'][$plugin_id]['options']['bid']) ? $tab['content'][$plugin_id]['options']['bid'] : '',
'#title' => $this
->t('Select a block'),
'#ajax' => [
'callback' => [
$this,
'blockTitleAjaxCallback',
],
'event' => 'change',
'progress' => [
'type' => 'throbber',
'message' => 'Please wait...',
],
'effect' => 'fade',
],
];
$form['block_title'] = [
'#type' => 'textfield',
'#default_value' => isset($tab['content'][$plugin_id]['options']['block_title']) ? $tab['content'][$plugin_id]['options']['block_title'] : '',
'#title' => $this
->t('Block Title'),
'#prefix' => '<div id="block-title-textfield-' . $tab['delta'] . '">',
'#suffix' => '</div>',
];
$form['display_title'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display block title'),
'#default_value' => isset($tab['content'][$plugin_id]['options']['display_title']) ? $tab['content'][$plugin_id]['options']['display_title'] : 0,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function render(array $tab) {
$options = $tab['content'][$tab['type']]['options'];
if (strpos($options['bid'], 'block_content') !== FALSE) {
$parts = explode(':', $options['bid']);
$entity_repository = \Drupal::service('entity.repository');
$block = $entity_repository
->loadEntityByUuid($parts[0], $parts[1]);
$entityTypeManager = \Drupal::entityTypeManager();
$block_content = $entityTypeManager
->getStorage('block_content')
->load($block
->id());
$render = $entityTypeManager
->getViewBuilder('block_content')
->view($block_content);
}
else {
$block_manager = \Drupal::service('plugin.manager.block');
// You can hard code configuration or you load from settings.
$config = [];
$plugin_block = $block_manager
->createInstance($options['bid'], $config);
// Some blocks might implement access check.
$access_result = $plugin_block
->access(\Drupal::currentUser(), TRUE);
// Return empty render array if user doesn't have access.
if ($access_result
->isForbidden()) {
return [];
}
$render = $plugin_block
->build();
}
return $render;
}
/**
* Get options for the block.
*/
private function getBlockOptions() {
$block_manager = \Drupal::service('plugin.manager.block');
$context_repository = \Drupal::service('context.repository');
// Only add blocks which work without any available context.
$definitions = $block_manager
->getDefinitionsForContexts($context_repository
->getAvailableContexts());
// Order by category, and then by admin label.
$definitions = $block_manager
->getSortedDefinitions($definitions);
$blocks = [];
foreach ($definitions as $block_id => $definition) {
$blocks[$block_id] = $definition['admin_label'] . ' (' . $definition['provider'] . ')';
}
return $blocks;
}
/**
* Ajax callback to change block title when block is selected.
*/
public function blockTitleAjaxCallback(array &$form, FormStateInterface $form_state) {
$tab_index = $form_state
->getTriggeringElement()['#array_parents'][2];
$element_id = '#block-title-textfield-' . $tab_index;
$selected_block = $form_state
->getValue('configuration_data')[$tab_index]['content']['block_content']['options']['bid'];
$block_manager = \Drupal::service('plugin.manager.block');
$context_repository = \Drupal::service('context.repository');
$definitions = $block_manager
->getDefinitionsForContexts($context_repository
->getAvailableContexts());
$form['block_title'] = [
'#type' => 'textfield',
'#value' => $definitions[$selected_block]['admin_label'],
'#title' => $this
->t('Block Title'),
'#prefix' => '<div id="block-title-textfield-' . $tab_index . '">',
'#suffix' => '</div>',
];
$form_state
->setRebuild(TRUE);
$ajax_response = new AjaxResponse();
$ajax_response
->addCommand(new ReplaceCommand($element_id, $form['block_title']));
return $ajax_response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlockContent:: |
public | function | Ajax callback to change block title when block is selected. | |
BlockContent:: |
private | function | Get options for the block. | |
BlockContent:: |
public | function |
Return form elements used on the edit/add from. Overrides TabTypeBase:: |
|
BlockContent:: |
public | function |
Return a render array for an individual tab tat the theme layer to process. Overrides TabTypeBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TabTypeBase:: |
protected | function | Gets the name of the plugin. |