Skip to content
Snippets Groups Projects
Commit 55c456dc authored by renatog's avatar renatog Committed by renatog
Browse files

Issue #3276124 by RenatoG: Implement early return instead of a long if and...

Issue #3276124 by RenatoG: Implement early return instead of a long if and reuse if to avoid multiple conditions
parent cd3434d8
Branches
Tags
10 merge requests!62Issue #3272848 by RenatoG: When the error appear the field is hidden and the...,!59Issue #3497768: Click JavaScript to close details causes modals to scroll unnecessarily,!35Issue #3366420: \Drupal calls should be avoided in classes,!34Issue #3366420: \Drupal calls should be avoided in classes,!29Issue #3272632 by RenatoG: Add a logic to block and disable the remove item if...,!28Issue #3272632 by RenatoG: Add a logic to block and disable the remove item if...,!23Issue #3272632 by RenatoG: Add a logic to block and disable the remove item if...,!22Issue #3272632 by RenatoG: Add a logic to block and disable the remove item if...,!20Issue #3272632 by RenatoG: Add a logic to block and disable the remove item if...,!7Issue #3320529 by justcaldwell: block_classes_stored setting only retains classes from most recently saved block
......@@ -506,364 +506,365 @@ class BlockClassHelperService {
$form_object = $form_state->getFormObject();
if ($form_object instanceof EntityFormInterface) {
$qty_classes_per_block = 10;
// Implement the alter only if is a instance of EntityFormInterface.
if (!($form_object instanceof EntityFormInterface)) {
return;
}
// Get config object.
$config = $this->configFactory->getEditable('block_class.settings');
$qty_classes_per_block = 10;
if (!empty($config->get('qty_classes_per_block'))) {
$qty_classes_per_block = $config->get('qty_classes_per_block');
}
// Get config object.
$config = $this->configFactory->getEditable('block_class.settings');
// Get the URL settings global page.
$url_settings_page = Url::fromRoute('block_class.settings')->toString();
if (!empty($config->get('qty_classes_per_block'))) {
$qty_classes_per_block = $config->get('qty_classes_per_block');
}
$url_used_items_list = Url::fromRoute('block_class.class_list')->toString();
// Get the URL settings global page.
$url_settings_page = Url::fromRoute('block_class.settings')->toString();
// Put the default help text.
$help_text = $this->t('Customize the styling of this block by adding CSS classes.');
$url_used_items_list = Url::fromRoute('block_class.class_list')->toString();
// Put the Modal with all items used.
$help_text .= ' <div class="show-items-used">' . $this->t('<a href="@url_used_items_list@" class="use-ajax" data-dialog-options="{&quot;width&quot;:800}" data-dialog-type="modal">Click here</a> to see all the classes used.</div>', [
'@url_used_items_list@' => $url_used_items_list,
]);
// Put the default help text.
$help_text = $this->t('Customize the styling of this block by adding CSS classes.');
$form['class'] = [
'#type' => 'details',
'#title' => $this->t('Class'),
'#open' => TRUE,
'#description' => $help_text,
];
// Put the Modal with all items used.
$help_text .= ' <div class="show-items-used">' . $this->t('<a href="@url_used_items_list@" class="use-ajax" data-dialog-options="{&quot;width&quot;:800}" data-dialog-type="modal">Click here</a> to see all the classes used.</div>', [
'@url_used_items_list@' => $url_used_items_list,
]);
// Put the weight only if exists.
if (!empty($config->get('weight_class')) || $config->get('weight_class') === '0') {
$form['class']['#weight'] = $config->get('weight_class');
}
$form['class'] = [
'#type' => 'details',
'#title' => $this->t('Class'),
'#open' => TRUE,
'#description' => $help_text,
];
/** @var \Drupal\block\BlockInterface $block */
$block = $form_object->getEntity();
// Put the weight only if exists.
if (!empty($config->get('weight_class')) || $config->get('weight_class') === '0') {
$form['class']['#weight'] = $config->get('weight_class');
}
// This will automatically be saved in the third party settings.
$form['class']['third_party_settings']['#tree'] = TRUE;
/** @var \Drupal\block\BlockInterface $block */
$block = $form_object->getEntity();
// Default field type.
$field_type = 'textfield';
// This will automatically be saved in the third party settings.
$form['class']['third_party_settings']['#tree'] = TRUE;
// Default value for maxlength.
$maxlength_block_class_field = 255;
// Default field type.
$field_type = 'textfield';
// Get the field type if exists.
if (!empty($config->get('field_type')) && $config->get('field_type') != 'multiple_textfields') {
$field_type = $config->get('field_type');
}
// Default value for maxlength.
$maxlength_block_class_field = 255;
// Get maxlength if exists.
if (!empty($config->get('maxlength_block_class_field'))) {
$maxlength_block_class_field = $config->get('maxlength_block_class_field');
}
// Get the field type if exists.
if (!empty($config->get('field_type')) && $config->get('field_type') != 'multiple_textfields') {
$field_type = $config->get('field_type');
}
$image_path = '/' . drupal_get_path('module', 'block_class') . '/images/';
// Get maxlength if exists.
if (!empty($config->get('maxlength_block_class_field'))) {
$maxlength_block_class_field = $config->get('maxlength_block_class_field');
}
if ($config->get('field_type') == 'textfield') {
$image_path = '/' . drupal_get_path('module', 'block_class') . '/images/';
// Remove the help text in the field group because the field will have
// their help text.
unset($form['class']['#description']);
if ($config->get('field_type') == 'textfield') {
$form['class']['third_party_settings']['block_class']['classes'] = [
'#type' => $field_type,
'#title' => $this->t('CSS class(es)'),
'#description' => $this->t('Customize the styling of this block by adding CSS classes. Separate multiple classes by spaces. The maxlength configured is @maxlength_block_class_field@. If necessary you can update it <a href="/admin/config/content/block-class/settings">here</a>. This class will appear in the first level of block. <a href="@image_path@/example-1.png">Click here</a> to see a example', [
'@maxlength_block_class_field@' => $maxlength_block_class_field,
'@image_path@' => $image_path,
]),
'#default_value' => $block->getThirdPartySetting('block_class', 'classes'),
'#maxlength' => $maxlength_block_class_field,
];
// Remove the help text in the field group because the field will have
// their help text.
unset($form['class']['#description']);
}
$form['class']['third_party_settings']['block_class']['classes'] = [
'#type' => $field_type,
'#title' => $this->t('CSS class(es)'),
'#description' => $this->t('Customize the styling of this block by adding CSS classes. Separate multiple classes by spaces. The maxlength configured is @maxlength_block_class_field@. If necessary you can update it <a href="/admin/config/content/block-class/settings">here</a>. This class will appear in the first level of block. <a href="@image_path@/example-1.png">Click here</a> to see a example', [
'@maxlength_block_class_field@' => $maxlength_block_class_field,
'@image_path@' => $image_path,
]),
'#default_value' => $block->getThirdPartySetting('block_class', 'classes'),
'#maxlength' => $maxlength_block_class_field,
];
if ($config->get('field_type') == 'multiple_textfields') {
}
// Get the classes on getThirdPartySettings.
$classes = $block->getThirdPartySetting('block_class', 'classes');
if ($config->get('field_type') == 'multiple_textfields') {
// Explode by spaces to get all classes in an array.
$classes = explode(' ', $classes);
// Get the classes on getThirdPartySettings.
$classes = $block->getThirdPartySetting('block_class', 'classes');
// If the quantity of items in the block is lower than the quantity of
// items configured in the settings we need to have the higher limit
// just to avoid some items being cut due the limit.
if ((int) $qty_classes_per_block < (int) count($classes)) {
$qty_classes_per_block = (int) count($classes);
}
// Explode by spaces to get all classes in an array.
$classes = explode(' ', $classes);
// Run a for to add 10 multiple fields.
for ($index = 0; $index <= ($qty_classes_per_block - 1); $index++) {
// If the quantity of items in the block is lower than the quantity of
// items configured in the settings we need to have the higher limit
// just to avoid some items being cut due the limit.
if ((int) $qty_classes_per_block < (int) count($classes)) {
$qty_classes_per_block = (int) count($classes);
}
// Initial value.
$multiclass_default_value = '';
// Run a for to add 10 multiple fields.
for ($index = 0; $index <= ($qty_classes_per_block - 1); $index++) {
// Verify if there is a value on this class to add in the field.
if (!empty($classes[$index])) {
$multiclass_default_value = $classes[$index];
}
// Initial value.
$multiclass_default_value = '';
// Insert the new field.
$form['class']['third_party_settings']['block_class']['classes_' . $index] = [
'#type' => 'textfield',
'#title' => $this->t('CSS class'),
'#default_value' => $multiclass_default_value,
'#maxlength' => $maxlength_block_class_field,
];
// Verify if there is a value on this class to add in the field.
if (!empty($classes[$index])) {
$multiclass_default_value = $classes[$index];
}
// Enable the auto-complete only is selected in the settings page.
if (!empty($config->get('enable_auto_complete'))) {
$form['class']['third_party_settings']['block_class']['classes_' . $index]['#autocomplete_route_name'] = 'block_class.autocomplete';
}
// Insert the new field.
$form['class']['third_party_settings']['block_class']['classes_' . $index] = [
'#type' => 'textfield',
'#title' => $this->t('CSS class'),
'#default_value' => $multiclass_default_value,
'#maxlength' => $maxlength_block_class_field,
];
// Insert a default class for all classes visible or not.
$form['class']['third_party_settings']['block_class']['classes_' . $index]['#attributes']['class'][] = 'multiple-textfield';
// Enable the auto-complete only is selected in the settings page.
if (!empty($config->get('enable_auto_complete'))) {
$form['class']['third_party_settings']['block_class']['classes_' . $index]['#autocomplete_route_name'] = 'block_class.autocomplete';
}
// Put the visible class by default.
$form['class']['third_party_settings']['block_class']['classes_' . $index]['#attributes']['class'][] = 'displayed-class-field';
// Insert a default class for all classes visible or not.
$form['class']['third_party_settings']['block_class']['classes_' . $index]['#attributes']['class'][] = 'multiple-textfield';
// If is the second o higher and there is no class for that, hide.
if ($index >= 1 && empty($classes[$index])) {
// Put the visible class by default.
$form['class']['third_party_settings']['block_class']['classes_' . $index]['#attributes']['class'][] = 'displayed-class-field';
$form['class']['third_party_settings']['block_class']['classes_' . $index]['#attributes']['class'][] = 'hidden-class-field';
// If is the second o higher and there is no class for that, hide.
if ($index >= 1 && empty($classes[$index])) {
// If this class should be hidden, get the key to put the right
// class on that.
$hidden_class_key = array_search('displayed-class-field', $form['class']['third_party_settings']['block_class']['classes_' . $index]['#attributes']['class']);
$form['class']['third_party_settings']['block_class']['classes_' . $index]['#attributes']['class'][] = 'hidden-class-field';
// Unset in the array to remove the hidden class.
unset($form['class']['third_party_settings']['block_class']['classes_' . $index]['#attributes']['class'][$hidden_class_key]);
// If this class should be hidden, get the key to put the right
// class on that.
$hidden_class_key = array_search('displayed-class-field', $form['class']['third_party_settings']['block_class']['classes_' . $index]['#attributes']['class']);
}
// Unset in the array to remove the hidden class.
unset($form['class']['third_party_settings']['block_class']['classes_' . $index]['#attributes']['class'][$hidden_class_key]);
}
// Add another item button in the last field.
$form['class']['third_party_settings']['block_class']['add_another_item'] = [
'#type' => 'button',
'#value' => 'Add another class',
];
// Add the class to identity the "add another item" button.
$form['class']['third_party_settings']['block_class']['add_another_item']['#attributes']['class'][] = 'block-class-add-another-item';
}
// Add remove item button in the last field.
$form['class']['third_party_settings']['block_class']['remove_item'] = [
'#type' => 'button',
'#value' => 'Remove class',
];
// Add another item button in the last field.
$form['class']['third_party_settings']['block_class']['add_another_item'] = [
'#type' => 'button',
'#value' => 'Add another class',
];
// Add the class to identity the "Remove item" button.
$form['class']['third_party_settings']['block_class']['remove_item']['#attributes']['class'][] = 'block-class-remove-item';
// Add the class to identity the "add another item" button.
$form['class']['third_party_settings']['block_class']['add_another_item']['#attributes']['class'][] = 'block-class-add-another-item';
// Verify if there is a help text for qty item and if not set a default
// value for this.
if (empty($help_text_qty_items)) {
$help_text_qty_items = '';
}
// Add remove item button in the last field.
$form['class']['third_party_settings']['block_class']['remove_item'] = [
'#type' => 'button',
'#value' => 'Remove class',
];
$help_text_qty_items .= ' ' . $this->t('The maximum of classes per block is @qty_classes_per_block@ but if you need you can update this value in the <a href="@url_settings_page@">settings page</a>', [
'@qty_classes_per_block@' => $qty_classes_per_block,
'@url_settings_page@' => $url_settings_page,
]);
// Add the class to identity the "Remove item" button.
$form['class']['third_party_settings']['block_class']['remove_item']['#attributes']['class'][] = 'block-class-remove-item';
$form['class']['third_party_settings']['block_class']['help_text_qty_items'] = [
'#type' => 'markup',
'#markup' => '<p class="help-text-qty-items help-text-qty-items-hidden">' . $help_text_qty_items . '</p>',
];
// Verify if there is a help text for qty item and if not set a default
// value for this.
if (empty($help_text_qty_items)) {
$help_text_qty_items = '';
}
$form['class']['third_party_settings']['block_class']['classes']['#attributes']['class'][] = 'block-class-class';
$help_text_qty_items .= ' ' . $this->t('The maximum of classes per block is @qty_classes_per_block@ but if you need you can update this value in the <a href="@url_settings_page@">settings page</a>', [
'@qty_classes_per_block@' => $qty_classes_per_block,
'@url_settings_page@' => $url_settings_page,
]);
if (!empty($config->get('enable_id_replacement'))) {
$form['class']['third_party_settings']['block_class']['help_text_qty_items'] = [
'#type' => 'markup',
'#markup' => '<p class="help-text-qty-items help-text-qty-items-hidden">' . $help_text_qty_items . '</p>',
];
}
// Default value for maxlength to be used in the replaced_id.
// If no value is present in the settings item we can use the default of
// 255 in the maxlength.
$maxlength_id = 255;
$form['class']['third_party_settings']['block_class']['classes']['#attributes']['class'][] = 'block-class-class';
// Get maxlength for replaced_id if exists in the Global Settings page.
if (!empty($config->get('maxlength_id'))) {
$maxlength_id = $config->get('maxlength_id');
}
if (!empty($config->get('enable_id_replacement'))) {
$form['replaced_id'] = [
'#type' => 'details',
'#title' => $this->t('ID'),
'#open' => TRUE,
'#description' => $this->t("Customize the block id"),
'#attributes' => [
'class' => [
'replaced-id-details',
],
// Default value for maxlength to be used in the replaced_id.
// If no value is present in the settings item we can use the default of
// 255 in the maxlength.
$maxlength_id = 255;
// Get maxlength for replaced_id if exists in the Global Settings page.
if (!empty($config->get('maxlength_id'))) {
$maxlength_id = $config->get('maxlength_id');
}
$form['replaced_id'] = [
'#type' => 'details',
'#title' => $this->t('ID'),
'#open' => TRUE,
'#description' => $this->t("Customize the block id"),
'#attributes' => [
'class' => [
'replaced-id-details',
],
];
],
];
// Put the weight only if exists.
if (!empty($config->get('weight_id')) || $config->get('weight_id') === '0') {
$form['replaced_id']['#weight'] = $config->get('weight_id');
}
// Put the weight only if exists.
if (!empty($config->get('weight_id')) || $config->get('weight_id') === '0') {
$form['replaced_id']['#weight'] = $config->get('weight_id');
}
$form['replaced_id']['third_party_settings']['#tree'] = TRUE;
$form['replaced_id']['third_party_settings']['#tree'] = TRUE;
$form['replaced_id']['third_party_settings']['block_class']['replaced_id'] = [
'#type' => 'textfield',
'#title' => $this->t('ID'),
'#description' => $this->t("If you put a value here it'll replace the block's id"),
'#default_value' => $block->getThirdPartySetting('block_class', 'replaced_id'),
'#maxlength' => $maxlength_id,
];
$form['replaced_id']['third_party_settings']['block_class']['replaced_id'] = [
'#type' => 'textfield',
'#title' => $this->t('ID'),
'#description' => $this->t("If you put a value here it'll replace the block's id"),
'#default_value' => $block->getThirdPartySetting('block_class', 'replaced_id'),
'#maxlength' => $maxlength_id,
];
// Insert the specific class for this item.
$form['replaced_id']['third_party_settings']['block_class']['replaced_id']['#attributes']['class'][] = 'replaced-id-item';
// Insert the specific class for this item.
$form['replaced_id']['third_party_settings']['block_class']['replaced_id']['#attributes']['class'][] = 'replaced-id-item';
}
}
if (!empty($config->get('enable_attributes'))) {
if (!empty($config->get('enable_attributes'))) {
$attributes = $block->getThirdPartySetting('block_class', 'attributes');
$attributes = $block->getThirdPartySetting('block_class', 'attributes');
$attributes = explode(PHP_EOL, $attributes);
$attributes = explode(PHP_EOL, $attributes);
$qty_attributes_per_block = 10;
$qty_attributes_per_block = 10;
if (!empty($config->get('qty_attributes_per_block'))) {
$qty_attributes_per_block = $config->get('qty_attributes_per_block');
}
if (!empty($config->get('qty_attributes_per_block'))) {
$qty_attributes_per_block = $config->get('qty_attributes_per_block');
}
// If the quantity of items in the block is lower than the quantity of
// items configured in the settings we need to have the higher limit
// just to avoid some items being cut due the limit.
if ((int) $qty_attributes_per_block < (int) count($attributes)) {
$qty_attributes_per_block = (int) count($attributes);
}
// If the quantity of items in the block is lower than the quantity of
// items configured in the settings we need to have the higher limit
// just to avoid some items being cut due the limit.
if ((int) $qty_attributes_per_block < (int) count($attributes)) {
$qty_attributes_per_block = (int) count($attributes);
}
// Add the attributes dynamically based on the settings field of global
// settings page.
for ($index = 0; $index <= ($qty_attributes_per_block - 1); $index++) {
// Add the attributes dynamically based on the settings field of global
// settings page.
for ($index = 0; $index <= ($qty_attributes_per_block - 1); $index++) {
// Default value for attribute key and attribute value.
$attribute_key = '';
$attribute_value = '';
// Default value for attribute key and attribute value.
$attribute_key = '';
$attribute_value = '';
// Verify if there an attribute to be filled.
if (!empty($attributes[$index])) {
// Verify if there an attribute to be filled.
if (!empty($attributes[$index])) {
$attribute = explode('|', $attributes[$index]);
$attribute = explode('|', $attributes[$index]);
// Get the attribute key and attribute value.
$attribute_key = $attribute[0];
$attribute_value = $attribute[1];
// Get the attribute key and attribute value.
$attribute_key = $attribute[0];
$attribute_value = $attribute[1];
}
}
// Get the URL of route with the attribute list used.
$url_used_attribute_list = Url::fromRoute('block_class.attribute_list')->toString();
// Get the URL of route with the attribute list used.
$url_used_attribute_list = Url::fromRoute('block_class.attribute_list')->toString();
// Create the help text for the multiple attribute fields.
$help_text = $this->t('Customize the this block by adding attributes. E.g. "data-block-type"="admin"');
// Create the help text for the multiple attribute fields.
$help_text = $this->t('Customize the this block by adding attributes. E.g. "data-block-type"="admin"');
// Update the help text with the Modal to show this for the user.
$help_text .= ' - <div class="show-items-used">' . $this->t('<a href="@url_used_attribute_list@" class="use-ajax" data-dialog-options="{&quot;width&quot;:800}" data-dialog-type="modal">Click here</a> to see all the attributes used.', [
'@url_used_attribute_list@' => $url_used_attribute_list,
]);
// Update the help text with the Modal to show this for the user.
$help_text .= ' - <div class="show-items-used">' . $this->t('<a href="@url_used_attribute_list@" class="use-ajax" data-dialog-options="{&quot;width&quot;:800}" data-dialog-type="modal">Click here</a> to see all the attributes used.', [
'@url_used_attribute_list@' => $url_used_attribute_list,
]);
// Create the field group for multiple attributes.
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index] = [
'#type' => 'details',
'#title' => $this->t('Attribute'),
'#open' => TRUE,
'#description' => $help_text,
'#attributes' => [
'class' => [
'attribute-details',
],
// Create the field group for multiple attributes.
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index] = [
'#type' => 'details',
'#title' => $this->t('Attribute'),
'#open' => TRUE,
'#description' => $help_text,
'#attributes' => [
'class' => [
'attribute-details',
],
];
// Put the weight only if exists.
if (!empty($config->get('weight_attributes')) || $config->get('weight_attributes') === '0') {
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['#weight'] = $config->get('weight_attributes');
}
// Default value for maxlength.
$maxlength_multiple_attributes = 255;
// Get maxlength if exists.
if (!empty($config->get('maxlength_attributes'))) {
$maxlength_multiple_attributes = $config->get('maxlength_attributes');
}
// Add the attribute key item.
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['attribute_key_' . $index] = [
'#type' => 'textfield',
'#title' => $this->t('Attribute Key'),
'#default_value' => $attribute_key,
'#description' => $this->t('Set the attribute key. E.g. data-block-type'),
'#maxlength' => $maxlength_multiple_attributes,
];
// Enable the auto-complete only is selected in the settings page.
if (!empty($config->get('enable_auto_complete'))) {
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['attribute_key_' . $index]['#autocomplete_route_name'] = 'block_class.autocomplete_attributes';
}
// Add the attribute value item.
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['attribute_value_' . $index] = [
'#type' => 'textfield',
'#title' => $this->t('Attribute Value'),
'#default_value' => $attribute_value,
'#description' => $this->t('Set the attribute value. E.g. admin'),
'#maxlength' => $maxlength_multiple_attributes,
];
// Enable the auto-complete only is selected in the settings page.
if (!empty($config->get('enable_auto_complete'))) {
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['attribute_value_' . $index]['#autocomplete_route_name'] = 'block_class.autocomplete_attribute_values';
}
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['#attributes']['class'][] = 'multiple-textfield-attribute';
// Show this attribute only if it's the first one of if already exists
// a value. (Editing the block).
if ($index == 0 || (!empty($attribute_key) && !empty($attribute_value))) {
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['#attributes']['class'][] = 'displayed-attribute-field';
}
else {
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['#attributes']['class'][] = 'hidden-attribute-field';
}
],
];
// Put the weight only if exists.
if (!empty($config->get('weight_attributes')) || $config->get('weight_attributes') === '0') {
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['#weight'] = $config->get('weight_attributes');
}
// Add the button to add another attribute item.
$form['multiple_attributes']['add_another_attribute'] = [
'#type' => 'button',
'#value' => 'Add another attribute',
// Default value for maxlength.
$maxlength_multiple_attributes = 255;
// Get maxlength if exists.
if (!empty($config->get('maxlength_attributes'))) {
$maxlength_multiple_attributes = $config->get('maxlength_attributes');
}
// Add the attribute key item.
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['attribute_key_' . $index] = [
'#type' => 'textfield',
'#title' => $this->t('Attribute Key'),
'#default_value' => $attribute_key,
'#description' => $this->t('Set the attribute key. E.g. data-block-type'),
'#maxlength' => $maxlength_multiple_attributes,
];
$form['multiple_attributes']['add_another_attribute']['#attributes']['class'][] = 'block-class-add-another-attribute';
// Enable the auto-complete only is selected in the settings page.
if (!empty($config->get('enable_auto_complete'))) {
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['attribute_key_' . $index]['#autocomplete_route_name'] = 'block_class.autocomplete_attributes';
}
// Add the button to remove an attribute item.
$form['multiple_attributes']['remove_attribute'] = [
'#type' => 'button',
'#value' => 'Remove attribute',
// Add the attribute value item.
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['attribute_value_' . $index] = [
'#type' => 'textfield',
'#title' => $this->t('Attribute Value'),
'#default_value' => $attribute_value,
'#description' => $this->t('Set the attribute value. E.g. admin'),
'#maxlength' => $maxlength_multiple_attributes,
];
$form['multiple_attributes']['remove_attribute']['#attributes']['class'][] = 'block-class-remove-attribute';
// Enable the auto-complete only is selected in the settings page.
if (!empty($config->get('enable_auto_complete'))) {
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['attribute_value_' . $index]['#autocomplete_route_name'] = 'block_class.autocomplete_attribute_values';
}
}
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['#attributes']['class'][] = 'multiple-textfield-attribute';
if (empty($block->getThirdPartySetting('block_class', 'classes')) && !empty($config->get('enable_auto_complete'))) {
$form['third_party_settings']['block_class']['classes']['#autocomplete_route_name'] = 'block_class.autocomplete';
// Show this attribute only if it's the first one of if already exists
// a value. (Editing the block).
if ($index == 0 || (!empty($attribute_key) && !empty($attribute_value))) {
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['#attributes']['class'][] = 'displayed-attribute-field';
}
else {
$form['multiple_attributes']['third_party_settings']['block_class']['attribute_' . $index]['#attributes']['class'][] = 'hidden-attribute-field';
}
}
// Add the button to add another attribute item.
$form['multiple_attributes']['add_another_attribute'] = [
'#type' => 'button',
'#value' => 'Add another attribute',
];
$form['multiple_attributes']['add_another_attribute']['#attributes']['class'][] = 'block-class-add-another-attribute';
// Add the button to remove an attribute item.
$form['multiple_attributes']['remove_attribute'] = [
'#type' => 'button',
'#value' => 'Remove attribute',
];
$form['multiple_attributes']['remove_attribute']['#attributes']['class'][] = 'block-class-remove-attribute';
}
if (empty($block->getThirdPartySetting('block_class', 'classes')) && !empty($config->get('enable_auto_complete'))) {
$form['third_party_settings']['block_class']['classes']['#autocomplete_route_name'] = 'block_class.autocomplete';
}
$form['#validate'][] = 'block_class_form_block_form_validate';
......@@ -1177,19 +1178,14 @@ class BlockClassHelperService {
}
// If there is a settings to allow only letters and numbers, validate it.
if (!empty($config->get('enable_special_chars'))) {
// Verify if there is a special char on this class.
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=+¬]/', $id_replacement)) {
$url_settings_page = Url::fromRoute('block_class.settings')->toString();
if (!empty($config->get('enable_special_chars')) && preg_match('/[\'^£$%&*()}{@#~?><>,|=+¬]/', $id_replacement)) {
// If there is a special chat return the error for the user.
$form_state->setErrorByName('replaced_id][third_party_settings][block_class][replaced_id', $this->t('Special chars is not enabled. To enable this, go to the <a href="@url_settings_page@">settings page</a>', [
'@url_settings_page@' => $url_settings_page,
]));
$url_settings_page = Url::fromRoute('block_class.settings')->toString();
}
// If there is a special chat return the error for the user.
$form_state->setErrorByName('replaced_id][third_party_settings][block_class][replaced_id', $this->t('Special chars is not enabled. To enable this, go to the <a href="@url_settings_page@">settings page</a>', [
'@url_settings_page@' => $url_settings_page,
]));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment