View source
<?php
namespace Drupal\image_effects\Plugin\image_effects\FontSelector;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file_mdm\FileMetadataManagerInterface;
use Drupal\image_effects\Plugin\ImageEffectsFontSelectorPluginInterface;
use Drupal\image_effects\Plugin\ImageEffectsPluginBase;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Basic extends ImageEffectsPluginBase implements ImageEffectsFontSelectorPluginInterface {
protected $fileMetadataManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, LoggerInterface $logger, FileMetadataManagerInterface $file_metadata_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $config_factory, $logger);
$this->fileMetadataManager = $file_metadata_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('logger.channel.image_effects'), $container
->get('file_metadata_manager'));
}
public function selectionElement(array $options = []) {
return array_merge([
'#type' => 'textfield',
'#title' => $this
->t('Font URI/path'),
'#description' => $this
->t('An URI, an absolute path, or a relative path. Relative paths will be resolved relative to the Drupal installation directory.'),
'#element_validate' => [
[
$this,
'validateSelectorUri',
],
],
], $options);
}
public function validateSelectorUri($element, FormStateInterface $form_state, $form) {
if (!empty($element['#value'])) {
if (!file_exists($element['#value'])) {
$form_state
->setErrorByName(implode('][', $element['#parents']), $this
->t('The file does not exist.'));
}
}
}
public function getDescription($uri) {
return $this->fileMetadataManager
->uri($uri)
->getMetadata('font', 'FullName');
}
}