class ImageWidgetHelper in Lightning Media 8.3
Same name and namespace in other branches
- 8.4 src/ImageWidgetHelper.php \Drupal\lightning_media\ImageWidgetHelper
- 8 src/ImageWidgetHelper.php \Drupal\lightning_media\ImageWidgetHelper
- 8.2 src/ImageWidgetHelper.php \Drupal\lightning_media\ImageWidgetHelper
Contains helper functions for manipulating image field widgets.
Hierarchy
- class \Drupal\lightning_media\ImageWidgetHelper
Expanded class hierarchy of ImageWidgetHelper
1 file declares its use of ImageWidgetHelper
- lightning_media.module in ./
lightning_media.module - Core media asset support for Lightning.
File
- src/
ImageWidgetHelper.php, line 10
Namespace
Drupal\lightning_mediaView source
class ImageWidgetHelper {
/**
* Returns normalized Lightning Media-specific settings for the widget.
*
* @param \Drupal\image\Plugin\Field\FieldWidget\ImageWidget $widget
* The widget plugin.
*
* @return array
* The normalized settings.
*/
protected static function getSettings(ImageWidget $widget) {
$settings = $widget
->getThirdPartySettings('lightning_media') ?: [];
$settings += [
'file_links' => TRUE,
'remove_button' => TRUE,
];
return $settings;
}
/**
* Returns the form for an image widget's Lightning Media-specific settings.
*
* @param \Drupal\image\Plugin\Field\FieldWidget\ImageWidget $widget
* The widget plugin.
*
* @return array
* The settings form elements.
*/
public static function getSettingsForm(ImageWidget $widget) {
$settings = static::getSettings($widget);
return [
'file_links' => [
'#type' => 'checkbox',
'#title' => t('Show links to uploaded files'),
'#default_value' => $settings['file_links'],
],
'remove_button' => [
'#type' => 'checkbox',
'#title' => t('Show Remove button'),
'#default_value' => $settings['remove_button'],
],
];
}
/**
* Summarizes an image widget's Lightning Media-specific settings.
*
* @param \Drupal\image\Plugin\Field\FieldWidget\ImageWidget $widget
* The widget plugin.
* @param array $summary
* (optional) An existing summary to augment.
*
* @return string[]
* The summarized settings.
*/
public static function summarize(ImageWidget $widget, array &$summary = NULL) {
$settings = static::getSettings($widget);
if (is_null($summary)) {
$summary = [];
}
if (empty($settings['file_links'])) {
$summary[] = t('Do not link to uploaded files');
}
if (empty($settings['remove_button'])) {
$summary[] = t('Hide Remove button');
}
return $summary;
}
/**
* Alters an image widget form element.
*
* @param array $element
* The widget form element.
* @param \Drupal\image\Plugin\Field\FieldWidget\ImageWidget $widget
* The widget plugin.
*/
public static function alter(array &$element, ImageWidget $widget) {
// Store the widget settings where process() can see them.
$element['#settings'] = static::getSettings($widget);
$element['#process'][] = [
static::class,
'process',
];
}
/**
* Process callback: does extra processing of an image widget form element.
*
* @param array $element
* The form element.
*
* @return array
* The processed form element.
*/
public static function process(array $element) {
$settings = $element['#settings'];
foreach ($element['fids']['#value'] as $fid) {
$element['file_' . $fid]['#access'] = $settings['file_links'];
}
$element['remove_button']['#access'] = $settings['remove_button'];
return $element;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ImageWidgetHelper:: |
public static | function | Alters an image widget form element. | |
ImageWidgetHelper:: |
protected static | function | Returns normalized Lightning Media-specific settings for the widget. | |
ImageWidgetHelper:: |
public static | function | Returns the form for an image widget's Lightning Media-specific settings. | |
ImageWidgetHelper:: |
public static | function | Process callback: does extra processing of an image widget form element. | |
ImageWidgetHelper:: |
public static | function | Summarizes an image widget's Lightning Media-specific settings. |