View source
<?php
namespace Drupal\exif\Controller;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldException;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\exif\ExifFactory;
use Drupal\media\Entity\MediaType;
use Drupal\taxonomy\Entity\Vocabulary;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class ExifSettingsController extends ControllerBase {
protected $entityDisplayRepository;
public function __construct(EntityDisplayRepositoryInterface $entity_display_repository) {
$this->entityDisplayRepository = $entity_display_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_display.repository'));
}
public function showGuide() {
return [
'#message' => "",
'#taxonomy' => 'http://drupal.org/handbook/modules/taxonomy/',
'#theme' => 'exif_helper_page',
'#attached' => [
'library' => [
'exif/exif-admin',
],
],
];
}
public function createPhotographyVocabulary() {
$values = [
"name" => "photographs metadata",
"vid" => "photographs_metadata",
"description" => "information related to photographs",
];
$voc = Vocabulary::load("photographs_metadata");
if (!$voc) {
Vocabulary::create($values)
->save();
$message = $this
->t('The vocabulary photography has been created');
}
else {
$message = $this
->t('The vocabulary photography is already created. nothing to do');
}
$this
->messenger()
->addMessage($message);
$response = new RedirectResponse('/admin/config/media/exif/helper');
$response
->send();
exit;
}
public function createPhotographyNodeType() {
$typeName = 'Photography';
$entity_type = 'node';
$machineName = strtolower($typeName);
try {
$storage = $this
->entityTypeManager()
->getStorage('node_type');
$type_definition = $storage
->load($machineName);
if (!$type_definition) {
$type_definition = $storage
->create([
'name' => $typeName,
'type' => $machineName,
'description' => 'Use Photography for content where the photo is the main content. You still can have some other information related to the photo itself.',
]);
$type_definition
->save();
}
$values = [
'targetEntityType' => $entity_type,
'bundle' => $machineName,
'mode' => 'default',
'status' => TRUE,
];
$this
->entityTypeManager()
->getStorage('entity_view_display')
->create($values);
$values = [
'targetEntityType' => $entity_type,
'bundle' => $machineName,
'mode' => 'default',
'status' => TRUE,
];
$this
->entityTypeManager()
->getStorage('entity_form_display')
->create($values);
$this
->addFields($entity_type, $type_definition);
$message = $this
->t('The %entitytype type %type has been fully created', [
'%entitytype' => $entity_type,
'%type' => $typeName,
]);
} catch (FieldException $fe) {
$message = $this
->t('An unexpected error was thrown during creation : ') . $fe
->getMessage();
}
$this
->messenger()
->addMessage($message);
$response = new RedirectResponse('/admin/config/media/exif/helper');
$response
->send();
exit;
}
public function createPhotographyMediaType() {
$typeName = 'Photography';
$entity_type = 'media';
$machineName = strtolower($typeName);
try {
if (interface_exists('\\Drupal\\media\\MediaInterface')) {
if (!($type_definition = MediaType::load($machineName))) {
$type_definition = MediaType::create([
'id' => $machineName,
'label' => $typeName,
'description' => 'Use Photography for content where the photo is the main content. You still can have some other information related to the photo itself.',
'source' => 'image',
'source_configuration' => [
'source_field' => 'field_image',
],
'field_map' => [],
'new_revision' => FALSE,
]);
$type_definition
->save();
}
$values = [
'targetEntityType' => $entity_type,
'bundle' => $machineName,
'mode' => 'default',
'status' => TRUE,
];
$this
->entityTypeManager()
->getStorage('entity_view_display')
->create($values);
$values = [
'targetEntityType' => $entity_type,
'bundle' => $machineName,
'mode' => 'default',
'status' => TRUE,
];
$this
->entityTypeManager()
->getStorage('entity_form_display')
->create($values);
$this
->addFields($entity_type, $type_definition);
$message = $this
->t('The %entitytype type %type has been fully created', [
'%entitytype' => $entity_type,
'%type' => $typeName,
]);
}
else {
$message = 'Nothing done. Media module is not present.';
}
} catch (FieldException $fe) {
$message = $this
->t('An unexpected error was thrown during creation : ') . $fe
->getMessage();
}
$this
->messenger()
->addMessage($message);
$response = new RedirectResponse('/admin/config/media/exif/helper');
$response
->send();
exit;
}
public function showSample() {
$sampleImageFilePath = drupal_get_path('module', 'exif') . '/sample.jpg';
$exif = ExifFactory::getExifInterface();
$fullmetadata = $exif
->readMetadataTags($sampleImageFilePath);
$html = '<table class="metadata-table"><tbody>';
foreach ($fullmetadata as $currentSection => $currentValues) {
$html .= '<tr class="metadata-section"><td colspan="2">' . $currentSection . '</td></tr>';
foreach ($currentValues as $currentKey => $currentValue) {
$exif_value = $this
->sanitizeValue($currentValue);
$html .= '<tr class="metadata-row ' . $currentKey . '"><td class="metadata-key">' . $currentKey . '</td><td class="metadata-value">' . $exif_value . '</td></tr>';
}
}
$html .= '</tbody><tfoot></tfoot></table>';
return [
'#metadata' => $html,
'#image_path' => '/' . $sampleImageFilePath,
'#taxo' => '',
'#permissionLink' => '',
'#taxonomyFragment' => '',
'#theme' => 'exif_sample',
'#attached' => [
'library' => [
'exif/exif-sample',
],
],
];
}
protected function addFields($entity_type, EntityInterface $type_definition) {
$this
->addFieldToEntityType($entity_type, $type_definition, 'Photo', 'image', 'image', 'exif_readonly');
$widget_settings = [
'image_field' => 'field_image',
'exif_field' => 'naming_convention',
];
$this
->addFieldToEntityType($entity_type, $type_definition, 'Creation date', 'exif_datetime', 'datetime', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'Photo Comment', 'exif_comments', 'text', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'Photo Description', 'exif_imagedescription', 'text', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'Photo Title', 'exif_title', 'text', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'Make', 'exif_make', 'text', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'Aperture', 'exif_aperturefnumber', 'text', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'Exposure', 'exif_exposuretime', 'text', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'ISO', 'exif_isospeedratings', 'text', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'Focal', 'exif_focallength', 'text', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'Flash', 'exif_flash', 'text', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'Exposure Program', 'exif_exposureprogram', 'text', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'Exposure Mode', 'exif_exposuremode', 'text', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'White Balance Mode', 'exif_whitebalance', 'text', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'scene Mode', 'exif_scenecapturetype', 'text', 'exif_readonly', $widget_settings);
$this
->addFieldToEntityType($entity_type, $type_definition, 'orientation', 'exif_orientation', 'text', 'exif_readonly', $widget_settings);
$this
->addReferenceToEntityType($entity_type, $type_definition, 'Photographer', 'exif_author', 'taxonomy_term', 'photographs_metadata', 'exif_readonly', $widget_settings);
$this
->addReferenceToEntityType($entity_type, $type_definition, 'Camera', 'exif_model', 'taxonomy_term', 'photographs_metadata', 'exif_readonly', $widget_settings);
$this
->addReferenceToEntityType($entity_type, $type_definition, 'ISO', 'exif_isospeedratings', 'taxonomy_term', 'photographs_metadata', 'exif_readonly', $widget_settings);
$widget_settings_for_tags = [
'image_field' => 'field_image',
'exif_field' => 'naming_convention',
'exif_field_separator' => ';',
];
$this
->addReferenceToEntityType($entity_type, $type_definition, 'Tags', 'exif_keywords', 'taxonomy_term', 'photographs_metadata', 'exif_readonly', $widget_settings_for_tags);
}
protected function addFieldToEntityType($entity_type, EntityInterface $type, $fieldLabel, $fieldName, $fieldType, $fieldWidget, array $widgetSettings = [], array $settings = []) {
$realFieldName = 'field_' . $fieldName;
$storage = $this
->getFieldStorageConfig();
$field_storage = $storage
->load($entity_type . '.' . $realFieldName);
if (empty($field_storage)) {
$field_storage_values = [
'field_name' => $realFieldName,
'field_label' => $fieldLabel,
'entity_type' => $entity_type,
'bundle' => $type
->id(),
'type' => $fieldType,
'translatable' => FALSE,
];
$storage
->create($field_storage_values)
->save();
}
$fieldSettings = [
'display_summary' => TRUE,
];
$this
->entityAddExtraField($entity_type, $type, $realFieldName, $fieldLabel, $fieldSettings, $fieldWidget, $widgetSettings);
}
protected function getFieldStorageConfig() {
return $this
->entityTypeManager()
->getStorage('field_storage_config');
}
protected function getFieldConfig() {
return $this
->entityTypeManager()
->getStorage('field_config');
}
protected function entityAddExtraField($entity_type, EntityInterface $type, $fieldName, $fieldLabel, array $fieldSettings, $fieldWidget, array $widgetSettings) {
$machineName = strtolower($fieldName);
$storage = $this
->getFieldStorageConfig();
$field_storage = $storage
->load($entity_type . '.' . $machineName);
$field_config = $this
->getFieldConfig();
$field = $field_config
->load($entity_type . '.' . $type
->id() . '.' . $machineName);
if (empty($field)) {
$field = $field_config
->create([
'field_storage' => $field_storage,
'bundle' => $type
->id(),
'label' => $fieldLabel,
'settings' => $fieldSettings,
]);
$field
->save();
}
$this
->entity_get_form_display($entity_type, $type
->id(), 'default')
->setComponent($machineName, [
'type' => $fieldWidget,
'settings' => $widgetSettings,
])
->save();
$this
->entity_get_display($entity_type, $type
->id(), 'default')
->setComponent($machineName, [
'label' => 'hidden',
'type' => 'text_default',
])
->save();
$view_modes = $this->entityDisplayRepository
->getViewModes($entity_type);
if (isset($view_modes['teaser'])) {
$this
->entity_get_display($entity_type, $type
->id(), 'teaser')
->setComponent($machineName, [
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
])
->save();
}
return $field;
}
protected function addReferenceToEntityType($entity_type, EntityInterface $type, $fieldLabel, $fieldName, $fieldType, $fieldTypeBundle, $fieldWidget, array $widgetSettings = [], array $settings = []) {
$realFieldName = 'field_' . $fieldName;
$storage = $this
->getFieldStorageConfig();
$field_storage = $storage
->load($entity_type . '.' . $realFieldName);
if (empty($field_storage)) {
$field_storage_values = [
'field_name' => $realFieldName,
'field_label' => $fieldLabel,
'entity_type' => $entity_type,
'bundle' => $type
->id(),
'type' => 'entity_reference',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'translatable' => FALSE,
'settings' => [
'target_type' => $fieldType,
],
];
$temp = $storage
->create($field_storage_values);
$temp
->save();
}
$fieldSettings = [
'handler' => 'default:' . $fieldType,
'handler_settings' => [
'target_bundles' => [
$fieldTypeBundle => $fieldTypeBundle,
],
],
'sort' => [
'field' => '_none',
],
'auto_create' => FALSE,
'auto_create_bundle' => '',
];
$this
->entityAddExtraField($entity_type, $type, $realFieldName, $fieldLabel, $fieldSettings, $fieldWidget, $widgetSettings);
}
protected function sanitizeValue($exif_value) {
if (!Unicode::validateUtf8($exif_value)) {
$exif_value = Html::escape(utf8_encode($exif_value));
}
return $exif_value;
}
protected function configureEntityFormDisplay($field_name, $widget_id = NULL) {
$options = $widget_id ? [
'type' => $widget_id,
] : [];
$this
->entity_get_form_display($this->entityTypeId, $this->bundle, 'default')
->setComponent($field_name, $options)
->save();
}
protected function configureEntityViewDisplay($field_name, $formatter_id = NULL) {
$options = $formatter_id ? [
'type' => $formatter_id,
] : [];
$this
->entity_get_display($this->entityTypeId, $this->bundle, 'default')
->setComponent($field_name, $options)
->save();
}
public function entity_get_form_display($entity_type, $bundle, $form_mode) {
$entity_form_display = EntityFormDisplay::load($entity_type . '.' . $bundle . '.' . $form_mode);
if (!$entity_form_display) {
$entity_form_display = EntityFormDisplay::create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $form_mode,
'status' => TRUE,
]);
}
return $entity_form_display;
}
public function entity_get_display($entity_type, $bundle, $view_mode) {
$display = EntityViewDisplay::load($entity_type . '.' . $bundle . '.' . $view_mode);
if (!$display) {
$display = EntityViewDisplay::create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $view_mode,
'status' => TRUE,
]);
}
return $display;
}
}