editor_advanced_image.module in Editor Advanced Image 8.2
Same filename and directory in other branches
Contains Editor Advanced Image module.
File
editor_advanced_image.moduleView source
<?php
/**
* @file
* Contains Editor Advanced Image module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
/**
* Implements hook_form_FORM_ID_alter().
*
* Add a title fields on EditorLinkDialog.
* Add an advanced dropdown containing:
* - class field
* - id field
* Note: the editor_file module declares that its
* Drupal\editor\Form\EditorImageDialog form uses
* 'editor_image_dialog' as base_form_id. In this case, the function above is
* going to be called as an implementation of hook_form_BASE_FORM_ID_alter().
*/
function editor_advanced_image_form_editor_image_dialog_alter(&$form, FormStateInterface $form_state, $form_id) {
$argument = $form_state
->getBuildInfo()['args'][0];
$settings = $argument
->getSettings();
// In case the only argument we get is the Editor instead of the FilterFormat.
if ($argument instanceof Editor) {
$argument = $argument
->getFilterFormat();
}
$restrictions = $argument
->getHtmlRestrictions();
if (isset($form_state
->getUserInput()['editor_object'])) {
$input = $form_state
->getUserInput()['editor_object'];
$form_state
->set('image_element', $input);
$form_state
->setCached(TRUE);
}
else {
// Retrieve the link element's attributes from form state.
$input = $form_state
->get('image_element') ?: [];
}
/*
* Helper to retrieve form fields' default values.
*
* @param string $attribute_name
* @param string $fallback
*
* @return mixed
* The existing value or the fallback.
*/
$get_default_value = function ($attribute_name, $fallback = '') use ($input) {
return !empty($input[$attribute_name]) ? $input[$attribute_name] : $fallback;
};
/*
* Helper to set the status of a form field according to the status of the
* filter about the attribute it is defining.
*
* @param string $attribute_name
*
* @return bool
* TRUE if the filter is disabled or if the entire "img" tag is allowed
* or if the given attribute is allowed for the "img" tag.
*/
$is_accessible = function ($attribute_name) use ($restrictions) {
return $restrictions === FALSE || $restrictions['allowed']['img'] === TRUE || !empty($restrictions['allowed']['img'][$attribute_name]);
};
$form['attributes']['title'] = [
'#type' => 'textfield',
'#title' => t('Title'),
'#description' => t('Populates the title attribute of the image, usually shown as a small tooltip on hover.'),
'#default_value' => $get_default_value('title'),
'#maxlength' => 1024,
'#weight' => 1,
'#access' => $is_accessible('title'),
];
$form['advanced'] = [
'#type' => 'details',
'#title' => t('Advanced'),
'#access' => FALSE,
'#weight' => 2,
];
$default_class = NULL;
if (!empty($settings['plugins']['editoradvancedimage']['default_class'])) {
$default_class = '<br>' . t('Default: <code>:classes</code>.', [
':classes' => $settings['plugins']['editoradvancedimage']['default_class'],
]);
}
$form['attributes']['class'] = [
'#type' => 'textfield',
'#title' => t('CSS classes'),
'#description' => t('List of CSS classes to add to the image, separated by spaces.') . $default_class,
'#default_value' => $get_default_value('class'),
'#maxlength' => 1024,
'#access' => $is_accessible('class'),
'#group' => 'advanced',
];
$form['attributes']['id'] = [
'#type' => 'textfield',
'#title' => t('ID'),
'#description' => t('Allows linking to this content using a <a href="https://en.wikipedia.org/wiki/Fragment_identifier">URL fragment</a>. Must be unique.'),
'#default_value' => $get_default_value('id'),
'#maxlength' => 1024,
'#access' => $is_accessible('id'),
'#group' => 'advanced',
];
// Show the advanced group if at least one of its fields is accessible.
foreach ($form['attributes'] as $element) {
if (!empty($element['#group']) && $element['#group'] === 'advanced' && (!isset($element['#access']) || $element['#access'] === TRUE)) {
$form['advanced']['#access'] = TRUE;
break;
}
}
// Add #validate callback that handles empty attributes.
array_unshift($form['#validate'], '_editor_advanced_image_attributes_validate');
}
/**
* Filter empty attributes to avoid empty HTML output.
*/
function _editor_advanced_image_attributes_validate(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValue('attributes');
$image_element = $form_state
->get('image_element');
foreach ($values as $key => $value) {
if (empty($value)) {
$form_state
->setValue([
'attributes',
$key,
], '');
// Special case on content creation.
if (empty($image_element)) {
$form_state
->unsetValue([
'attributes',
$key,
]);
}
}
}
}
Functions
Name | Description |
---|---|
editor_advanced_image_form_editor_image_dialog_alter | Implements hook_form_FORM_ID_alter(). |
_editor_advanced_image_attributes_validate | Filter empty attributes to avoid empty HTML output. |