You are here

class ImageWidgetHelper in Lightning Media 8.3

Same name and namespace in other branches
  1. 8.4 src/ImageWidgetHelper.php \Drupal\lightning_media\ImageWidgetHelper
  2. 8 src/ImageWidgetHelper.php \Drupal\lightning_media\ImageWidgetHelper
  3. 8.2 src/ImageWidgetHelper.php \Drupal\lightning_media\ImageWidgetHelper

Contains helper functions for manipulating image field widgets.

Hierarchy

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_media
View 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

Namesort descending Modifiers Type Description Overrides
ImageWidgetHelper::alter public static function Alters an image widget form element.
ImageWidgetHelper::getSettings protected static function Returns normalized Lightning Media-specific settings for the widget.
ImageWidgetHelper::getSettingsForm public static function Returns the form for an image widget's Lightning Media-specific settings.
ImageWidgetHelper::process public static function Process callback: does extra processing of an image widget form element.
ImageWidgetHelper::summarize public static function Summarizes an image widget's Lightning Media-specific settings.