ImageWidget.php in Panopoly 8.2
File
modules/panopoly/panopoly_media/src/Plugin/Field/FieldWidget/ImageWidget.php
View source
<?php
namespace Drupal\panopoly_media\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\image\Plugin\Field\FieldWidget\ImageWidget as CoreImageWidget;
class ImageWidget extends CoreImageWidget {
public static function process($element, FormStateInterface $form_state, $form) {
$element = parent::process($element, $form_state, $form);
$entityType = $element['#entity_type'];
if ($entityType != 'media') {
return $element;
}
if (!empty($element['#files'])) {
$files = $element['#files'];
$file = reset($files);
$iptc = self::getDataService()
->getData($file
->getFileUri());
$iptc['name'] = empty($iptc['title']) ? $file
->getFilename() : $iptc['title'];
$parents = array_slice($element['#parents'], 0, -2);
$element['#attached']['library'][] = 'panopoly_media/img_data';
$element['panopoly_media_img_data'] = [
'#type' => 'container',
'#attributes' => [
'data-type' => [
'panopolyMediaImgData',
],
'data-entity-type' => $entityType,
'data-form-parents' => $parents,
],
];
foreach (array_filter($iptc) as $property => $value) {
$element['panopoly_media_img_data']['#attributes']['data-iptc-' . $property] = $value;
}
$map = static::getDataService()
->getElementMap($entityType);
$element['#attached']['drupalSettings']['panopolyMediaImgDataMap'][$entityType] = $map;
static::getModuleHandler()
->alter('panopoly_media_iptc_values', $iptc, $entityType);
foreach ($map as $mapData) {
if (isset($mapData['element']) && !empty($iptc[$mapData['iptc']])) {
$name = array_merge($mapData['element'], [
'#default_value',
]);
$val = NestedArray::getValue($element, $name);
if (empty($val)) {
NestedArray::setValue($element, $name, $iptc[$mapData['iptc']]);
}
}
}
}
return $element;
}
protected static function getModuleHandler() {
return \Drupal::moduleHandler();
}
protected static function getDataService() {
return \Drupal::service('panopoly_media.img_data');
}
}