Skip to content
Snippets Groups Projects
Commit 3a817539 authored by Nikita Sineok's avatar Nikita Sineok Committed by howard ge
Browse files

Issue #3038638 by nikita_tt, dinazaur, g089h515r806: Ability to specify #id for particular Tab

parent 56c0e1e1
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@
* Provides block tabs.
*/
use Drupal\blocktabs\ConfigurableTabInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Render\Markup;
use Drupal\Component\Utility\Xss;
......@@ -60,6 +61,7 @@ function blocktabs_theme_suggestions_blocktabs(array $variables) {
* Prepares variables for blocktabs templates.
*/
function template_preprocess_blocktabs(&$variables) {
/** @var \Drupal\blocktabs\Entity\Blocktabs $blocktabs */
$blocktabs = $variables['elements']['#blocktabs'];
$tabs_id = 'blocktabs-' . $blocktabs->id();
......@@ -80,6 +82,12 @@ function template_preprocess_blocktabs(&$variables) {
foreach ($tabs as $tab) {
$tab_id = $tabs_id . '-' . $tab->getWeight();
// Changes #id for the tab.
if ($tab instanceof ConfigurableTabInterface && !empty($tab->getTabCssId())) {
$tab_id = $tab->getTabCssId();
}
$tab_obj = new stdClass();
$tab_obj->title = [
'#markup' => $tab->getTitle(),
......
......@@ -2,6 +2,8 @@
namespace Drupal\blocktabs;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
/**
......@@ -16,16 +18,49 @@ use Drupal\Core\Form\FormStateInterface;
*/
abstract class ConfigurableTabBase extends TabBase implements ConfigurableTabInterface {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return ['tab_css_id' => NULL]
+ parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['tab_css_id'] = [
'#type' => 'textfield',
'#title' => $this->t('Tab CSS id'),
'#description' => $this->t("Leave empty if you don't want to change default."),
'#default_value' => $this->configuration['tab_css_id'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$tab_css_id = $form_state->getValue('tab_css_id');
if ($tab_css_id && $tab_css_id != Html::getId($tab_css_id)) {
$form_state->setError($form['data']['tab_css_id'], $this->t('The CSS id is not valid. Suggest using this #id: %suggestion', ['%suggestion' => Html::getId($tab_css_id)]));
}
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['tab_css_id'] = $form_state->getValue('tab_css_id');
}
/**
* {@inheritdoc}
*/
public function getTabCssId() {
return NestedArray::getValue($this->configuration, ['tab_css_id']);
}
}
......@@ -15,4 +15,13 @@ use Drupal\Core\Plugin\PluginFormInterface;
* @see plugin_api
*/
interface ConfigurableTabInterface extends TabInterface, PluginFormInterface {
/**
* Return CSS ID for the tab.
*
* @return string|null
* CSS ID or NULL if not set.
*/
public function getTabCssId();
}
......@@ -115,6 +115,12 @@ abstract class TabFormBase extends FormBase {
// pass that through for validation.
$tab_data = (new FormState())->setValues($form_state->getValue('data'));
$this->tab->validateConfigurationForm($form, $tab_data);
// Merges errors.
foreach ($tab_data->getErrors() as $error_element_name => $error_message) {
$form_state->setErrorByName($error_element_name, $error_message);
}
// Update the original form values.
$form_state->setValue('data', $tab_data->getValues());
}
......@@ -129,6 +135,7 @@ abstract class TabFormBase extends FormBase {
// pass that through for submission.
$tab_data = (new FormState())->setValues($form_state->getValue('data'));
$this->tab->submitConfigurationForm($form, $tab_data);
// $logger = \Drupal::logger('blocktabs');
// $logger->notice('submitForm:' . var_export($tab_data, true));
// $logger->notice('default_view_name:' .
......
......@@ -43,13 +43,15 @@ class BlockContentTab extends ConfigurableTabBase {
public function defaultConfiguration() {
return [
'block_uuid' => NULL,
];
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$sql = "SELECT bd.info, b.uuid FROM {block_content_field_data} bd LEFT JOIN {block_content} b ON bd.id = b.id";
$result = \Drupal::database()->query($sql);
$block_uuid_options = [
......
......@@ -92,13 +92,15 @@ class BlockTab extends ConfigurableTabBase {
return [
'block_id' => NULL,
'config' => [],
];
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$definitions = $this->blockManager->getGroupedDefinitions();
$options = [];
......
......@@ -52,13 +52,15 @@ class ViewsTab extends ConfigurableTabBase {
'view_display' => NULL,
'view_arg' => NULL,
'display_tab' => NULL,
];
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$view_options = Views::getViewsAsOptions(TRUE, 'enabled', NULL, FALSE, TRUE);
$user_input = $form_state->getUserInput();
$data = $user_input['data'] ?? [];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment