public function ImagemagickToolkit::buildConfigurationForm in ImageMagick 8
Same name and namespace in other branches
- 8.3 src/Plugin/ImageToolkit/ImagemagickToolkit.php \Drupal\imagemagick\Plugin\ImageToolkit\ImagemagickToolkit::buildConfigurationForm()
- 8.2 src/Plugin/ImageToolkit/ImagemagickToolkit.php \Drupal\imagemagick\Plugin\ImageToolkit\ImagemagickToolkit::buildConfigurationForm()
Form constructor.
Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.
Parameters
array $form: An associative array containing the initial structure of the plugin form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().
Return value
array The form structure.
Overrides PluginFormInterface::buildConfigurationForm
File
- src/
Plugin/ ImageToolkit/ ImagemagickToolkit.php, line 179
Class
- ImagemagickToolkit
- Provides ImageMagick integration toolkit for image manipulation.
Namespace
Drupal\imagemagick\Plugin\ImageToolkitCode
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$config = $this->configFactory
->getEditable('imagemagick.settings');
$form['imagemagick'] = [
'#markup' => $this
->t("<a href=':im-url'>ImageMagick</a> and <a href=':gm-url'>GraphicsMagick</a> are stand-alone packages for image manipulation. At least one of them must be installed on the server, and you need to know where it is located. Consult your server administrator or hosting provider for details.", [
':im-url' => 'http://www.imagemagick.org',
':gm-url' => 'http://www.graphicsmagick.org',
]),
];
$form['quality'] = [
'#type' => 'number',
'#title' => $this
->t('Image quality'),
'#size' => 10,
'#min' => 0,
'#max' => 100,
'#maxlength' => 3,
'#default_value' => $config
->get('quality'),
'#field_suffix' => '%',
'#description' => $this
->t('Define the image quality of processed images. Ranges from 0 to 100. Higher values mean better image quality but bigger files.'),
];
// Graphics suite to use.
$form['suite'] = [
'#type' => 'details',
'#open' => TRUE,
'#collapsible' => FALSE,
'#title' => $this
->t('Graphics package'),
];
$options = [
'imagemagick' => $this
->getPackageLabel('imagemagick'),
'graphicsmagick' => $this
->getPackageLabel('graphicsmagick'),
];
$form['suite']['binaries'] = [
'#type' => 'radios',
'#title' => $this
->t('Suite'),
'#default_value' => $this
->getPackage(),
'#options' => $options,
'#required' => TRUE,
'#description' => $this
->t("Select the graphics package to use."),
];
// Path to binaries.
$form['suite']['path_to_binaries'] = [
'#type' => 'textfield',
'#title' => $this
->t('Path to the package executables'),
'#default_value' => $config
->get('path_to_binaries'),
'#required' => FALSE,
'#description' => $this
->t('If needed, the path to the package executables (<kbd>convert</kbd>, <kbd>identify</kbd>, <kbd>gm</kbd>, etc.), <b>including</b> the trailing slash/backslash. For example: <kbd>/usr/bin/</kbd> or <kbd>C:\\Program Files\\ImageMagick-6.3.4-Q16\\</kbd>.'),
];
// Version information.
$status = $this
->checkPath($config
->get('path_to_binaries'));
if (empty($status['errors'])) {
$version_info = explode("\n", preg_replace('/\\r/', '', Html::escape($status['output'])));
}
else {
$version_info = $status['errors'];
}
$form['suite']['version'] = [
'#type' => 'details',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => $this
->t('Version information'),
'#description' => '<pre>' . implode('<br />', $version_info) . '</pre>',
];
// Image formats.
$form['formats'] = [
'#type' => 'details',
'#open' => TRUE,
'#collapsible' => FALSE,
'#title' => $this
->t('Image formats'),
];
// Use 'identify' command.
$form['formats']['use_identify'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use "identify"'),
'#default_value' => $config
->get('use_identify'),
'#description' => $this
->t('Use the <kbd>identify</kbd> command to parse image files to determine image format and dimensions. If not selected, the PHP <kbd>getimagesize</kbd> function will be used, BUT this will limit the image formats supported by the toolkit.'),
];
// Image formats enabled in the toolkit.
$form['formats']['enabled'] = [
'#type' => 'item',
'#title' => $this
->t('Enabled images'),
'#description' => $this
->t("@suite formats: %formats<br />Image file extensions: %extensions", [
'%formats' => implode(', ', $this->formatMapper
->getEnabledFormats()),
'%extensions' => Unicode::strtolower(implode(', ', static::getSupportedExtensions())),
'@suite' => $this
->getPackageLabel(),
]),
];
// Image formats map.
$form['formats']['mapping'] = [
'#type' => 'details',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => $this
->t('Enable/disable image formats'),
'#description' => $this
->t("Edit the map below to enable/disable image formats. Enabled image file extensions will be determined by the enabled formats, through their MIME types. More information in the module's README.txt"),
];
$form['formats']['mapping']['image_formats'] = [
'#type' => 'textarea',
'#rows' => 15,
'#default_value' => Yaml::encode($config
->get('image_formats')),
];
// Image formats supported by the package.
if (empty($status['errors'])) {
$this
->addArgument('-list format');
$this
->imagemagickExec('convert', $output);
$this
->resetArguments();
$formats_info = implode('<br />', explode("\n", preg_replace('/\\r/', '', Html::escape($output))));
$form['formats']['list'] = [
'#type' => 'details',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => $this
->t('Format list'),
'#description' => $this
->t("Supported image formats returned by executing <kbd>'convert -list format'</kbd>. <b>Note:</b> these are the formats supported by the installed @suite executable, <b>not</b> by the toolkit.<br /><br />", [
'@suite' => $this
->getPackageLabel(),
]),
];
$form['formats']['list']['list'] = [
'#markup' => "<pre>" . $formats_info . "</pre>",
];
}
// Execution options.
$form['exec'] = [
'#type' => 'details',
'#open' => TRUE,
'#collapsible' => FALSE,
'#title' => $this
->t('Execution options'),
];
// Prepend arguments.
$form['exec']['prepend'] = [
'#type' => 'textfield',
'#title' => $this
->t('Prepend arguments'),
'#default_value' => $config
->get('prepend'),
'#required' => FALSE,
'#description' => $this
->t('Use this to add e.g. <kbd>-limit</kbd> or <kbd>-debug</kbd> arguments in front of the others when executing the <kbd>identify</kbd> and <kbd>convert</kbd> commands.'),
];
// Locale.
$form['exec']['locale'] = [
'#type' => 'textfield',
'#title' => $this
->t('Locale'),
'#default_value' => $config
->get('locale'),
'#required' => FALSE,
'#description' => $this
->t("The locale to be used to prepare the command passed to executables. The default, <kbd>'en_US.UTF-8'</kbd>, should work in most cases. If that is not available on the server, enter another locale. On *nix servers, type <kbd>'locale -a'</kbd> in a shell window to see a list of all locales available."),
];
// Log warnings.
$form['exec']['log_warnings'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Log warnings'),
'#default_value' => $config
->get('log_warnings'),
'#description' => $this
->t('Log a warning entry in the watchdog when the execution of a command returns with a non-zero code, but no error message.'),
];
// Debugging.
$form['exec']['debug'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display debugging information'),
'#default_value' => $config
->get('debug'),
'#description' => $this
->t('Shows commands and their output to users with the %permission permission.', [
'%permission' => $this
->t('Administer site configuration'),
]),
];
// Advanced image settings.
$form['advanced'] = [
'#type' => 'details',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => $this
->t('Advanced image settings'),
];
$form['advanced']['density'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Change image resolution to 72 ppi'),
'#default_value' => $config
->get('advanced.density'),
'#return_value' => 72,
'#description' => $this
->t("Resamples the image <a href=':help-url'>density</a> to a resolution of 72 pixels per inch, the default for web images. Does not affect the pixel size or quality.", [
':help-url' => 'http://www.imagemagick.org/script/command-line-options.php#density',
]),
];
$form['advanced']['colorspace'] = [
'#type' => 'select',
'#title' => $this
->t('Convert colorspace'),
'#default_value' => $config
->get('advanced.colorspace'),
'#options' => [
'RGB' => $this
->t('RGB'),
'sRGB' => $this
->t('sRGB'),
'GRAY' => $this
->t('Gray'),
],
'#empty_value' => 0,
'#empty_option' => $this
->t('- Original -'),
'#description' => $this
->t("Converts processed images to the specified <a href=':help-url'>colorspace</a>. The color profile option overrides this setting.", [
':help-url' => 'http://www.imagemagick.org/script/command-line-options.php#colorspace',
]),
'#states' => [
'enabled' => [
':input[name="imagemagick[advanced][profile]"]' => [
'value' => '',
],
],
],
];
$form['advanced']['profile'] = [
'#type' => 'textfield',
'#title' => $this
->t('Color profile path'),
'#default_value' => $config
->get('advanced.profile'),
'#description' => $this
->t("The path to a <a href=':help-url'>color profile</a> file that all processed images will be converted to. Leave blank to disable. Use a <a href=':color-url'>sRGB profile</a> to correct the display of professional images and photography.", [
':help-url' => 'http://www.imagemagick.org/script/command-line-options.php#profile',
':color-url' => 'http://www.color.org/profiles.html',
]),
];
return $form;
}