You are here

class ExifSettingsForm in Exif 8.2

Same name and namespace in other branches
  1. 8 src/Controller/ExifSettingsForm.php \Drupal\exif\Controller\ExifSettingsForm

Manage Settings forms.

@package Drupal\exif\Controller

Hierarchy

Expanded class hierarchy of ExifSettingsForm

1 string reference to 'ExifSettingsForm'
exif.routing.yml in ./exif.routing.yml
exif.routing.yml

File

src/Controller/ExifSettingsForm.php, line 21

Namespace

Drupal\exif\Controller
View source
class ExifSettingsForm extends ConfigFormBase implements ContainerInjectionInterface {

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs the ExifSettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The configuration factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity manager.
   */
  public function __construct(ConfigFactoryInterface $configFactory, EntityTypeManagerInterface $entity_type_manager) {
    ConfigFormBase::__construct($configFactory);
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('entity.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'exif_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('exif.settings');
    $form['information1'] = [
      '#type' => 'item',
      '#title' => 'Informations',
      '#markup' => t('If you have not create a media/content type for your photographies') . ', ' . t('take a look at <a href="/admin/config/media/exif/helper">the quick start page</a>.'),
    ];
    $form['information2'] = [
      '#type' => 'item',
      '#title' => '',
      '#markup' => t('To have a sample of metadata content, take a look at <a href="/admin/config/media/exif/sample">the sample page</a>.'),
    ];
    $form['exif'] = [
      '#type' => 'vertical_tabs',
      '#prefix' => '<div class="exif">',
      '#suffix' => '</div>',
      '#title' => t('Image Metadata Settings'),
      '#description' => t('If you have not create a media/content type for your photographies') . ', ' . t('take a look at <a href="/admin/config/media/exif/helper">the quick start page</a>.'),
    ];
    $form['global'] = [
      '#type' => 'details',
      '#title' => t('Global Settings'),
      '#group' => 'exif',
    ];
    $form['global']['granularity'] = [
      '#type' => 'select',
      '#title' => t('Granularity'),
      '#options' => [
        0 => t('Default'),
        1 => 'Day',
      ],
      '#default_value' => $config
        ->get('granularity', 0),
      '#description' => t('If a timestamp is selected (for example the date the picture was taken), you can specify here how granular the timestamp should be. If you select default it will just take whatever is available in the picture. If you select Day, the Date saved will look something like 13-12-2008. This can be useful if you want to use some kind of grouping on the data.'),
    ];
    $form['fieldname'] = [
      '#type' => 'markup',
      '#value' => "My Value Goes Here",
    ];
    $form['node'] = [
      '#type' => 'details',
      '#title' => t('Content types'),
      '#group' => 'exif',
    ];
    $all_nodetypes = $this->entityTypeManager
      ->getStorage('node_type')
      ->loadMultiple();
    $all_nt = [];
    foreach ($all_nodetypes as $item) {
      $all_nt[$item
        ->id()] = $item
        ->label();
    }
    $form['node']['nodetypes'] = [
      '#type' => 'checkboxes',
      '#title' => t('Nodetypes'),
      '#options' => $all_nt,
      '#default_value' => $config
        ->get('nodetypes', []),
      '#description' => t('Select nodetypes which should be checked for iptc & exif data.'),
    ];

    // The old way (still in use so keep it).
    if (Drupal::moduleHandler()
      ->moduleExists("file_entity")) {
      $form['file'] = [
        '#type' => 'details',
        '#title' => t('File types'),
        '#group' => 'exif',
      ];
      $all_mt = [];
      $all_filetypes = FileType::loadEnabled();

      // Setup file types.
      foreach ($all_filetypes as $item) {
        $all_mt[$item
          ->id()] = $item
          ->label();
      }
      $form['file']['filetypes'] = [
        '#type' => 'checkboxes',
        '#title' => t('Filetypes'),
        '#options' => $all_mt,
        '#default_value' => $config
          ->get('filetypes', []),
        '#description' => t('Select filetypes which should be checked for itpc & exif data.'),
      ];
    }
    else {
      $form['file']['filetypes'] = [
        '#type' => 'hidden',
        '#value' => [],
      ];
    }
    if (interface_exists('\\Drupal\\media\\MediaInterface')) {
      $form['media'] = [
        '#type' => 'details',
        '#title' => t('Media types'),
        '#group' => 'exif',
      ];
      $all_mediatypes = $this->entityTypeManager
        ->getStorage('media_type')
        ->loadMultiple();
      $all_mt = [];
      foreach ($all_mediatypes as $item) {
        $all_mt[$item
          ->id()] = $item
          ->label();
      }
      $form['media']['mediatypes'] = [
        '#type' => 'checkboxes',
        '#title' => t('Mediatypes'),
        '#options' => $all_mt,
        '#default_value' => $config
          ->get('mediatypes', []),
        '#description' => t('Select mediatypes which should be checked for iptc & exif data.'),
      ];
    }
    else {
      $form['media']['mediatypes'] = [
        '#type' => 'hidden',
        '#default_value' => $config
          ->get('mediatypes', []),
      ];
    }
    $form['global']['update_metadata'] = [
      '#type' => 'checkbox',
      '#title' => t('Refresh on node update'),
      '#default_value' => $config
        ->get('update_metadata', TRUE),
      '#description' => t('If media/exif enable this option, Exif data is being updated when the node is being updated.'),
    ];
    $form['global']['extraction_solution'] = [
      '#type' => 'select',
      '#title' => t('which extraction solution to use on node update'),
      '#options' => ExifFactory::getExtractionSolutions(),
      '#default_value' => $config
        ->get('extraction_solution', "php_extensions"),
      '#description' => t('If media/exif enable this option, Exif data is being updated when the node is being updated.'),
    ];
    $form['global']['exiftool_location'] = [
      '#type' => 'textfield',
      '#title' => t('location of exiftool binary'),
      '#default_value' => $config
        ->get('exiftool_location', "exiftool"),
      '#description' => t('where is the exiftool binaries (only needed if extraction solution chosen is exiftool)'),
    ];
    $form['global']['write_empty_values'] = [
      '#type' => 'checkbox',
      '#title' => t('Write empty image data?'),
      '#default_value' => $config
        ->get('write_empty_values', TRUE),
      '#description' => t("If checked all values will be written. So for example if you want to read the creation date from EXIF, but it's not available, it will just write an empty string. If unchecked, empty strings will not be written. This might be the desired behavior, if you have a default value for the CCK field."),
    ];
    $all_vocabularies = Vocabulary::loadMultiple();
    $all_vocs = [];
    $all_vocs[0] = 'None';
    foreach ($all_vocabularies as $item) {
      $all_vocs[$item
        ->id()] = $item
        ->label();
    }
    $form['global']['vocabulary'] = [
      '#type' => 'select',
      '#title' => t('Default Vocabulary'),
      '#options' => $all_vocs,
      '#default_value' => $config
        ->get('vocabulary', []),
      '#description' => t('Select vocabulary which should be used for iptc & exif data.') . t('If you think no vocabulary is usable for the purpose') . ', ' . t('take a look at <a href="/admin/config/media/exif/helper">the quick start page</a>.'),
    ];

    // TODO : Check if the media module is install to add automatically
    // TODO : the image type active and add active default exif field.
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getValue('extraction_solution') == 'simple_exiftool' && !is_executable($form_state
      ->getValue('exiftool_location'))) {
      $form_state
        ->setErrorByName('exiftool_location', $this
        ->t('The location provided for exiftool is not correct. Please ensure the exiftool location exists and is executable.'));
    }
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    foreach ([
      'nodetypes',
      'filetypes',
      'mediatypes',
    ] as $entity_type) {
      $value = $form_state
        ->getValue($entity_type);
      if (empty($value)) {
        $value = [];
      }
      $this
        ->config('exif.settings')
        ->set($entity_type, $value);
    }
    $this
      ->config('exif.settings')
      ->set('update_metadata', $form_state
      ->getValue('update_metadata', TRUE))
      ->set('write_empty_values', $form_state
      ->getValue('write_empty_values', FALSE))
      ->set('vocabulary', $form_state
      ->getValue('vocabulary', "0"))
      ->set('granularity', $form_state
      ->getValue('granularity'))
      ->set('date_format_exif', $form_state
      ->getValue('date_format_exif'))
      ->set('extraction_solution', $form_state
      ->getValue('extraction_solution'))
      ->set('exiftool_location', $form_state
      ->getValue('exiftool_location'))
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'exif.settings',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
ExifSettingsForm::$entityTypeManager protected property The entity manager.
ExifSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ExifSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
ExifSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ExifSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ExifSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ExifSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
ExifSettingsForm::__construct public function Constructs the ExifSettingsForm object. Overrides ConfigFormBase::__construct
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.