diff --git a/config/install/image.style.embed_button.yml b/config/install/image.style.embed_button.yml new file mode 100644 index 0000000..3f5b399 --- /dev/null +++ b/config/install/image.style.embed_button.yml @@ -0,0 +1,14 @@ +name: embed_button +label: 'Embed Button (32x32)' +effects: + 0cea8388-fe1f-4202-af26-385bad55e186: + id: image_scale_and_crop + data: + width: 32 + height: 32 + weight: 0 + uuid: 0cea8388-fe1f-4202-af26-385bad55e186 +langcode: en +dependencies: + module: + - image diff --git a/embed.info.yml b/embed.info.yml index aa7c120..e37e9d1 100644 --- a/embed.info.yml +++ b/embed.info.yml @@ -2,3 +2,5 @@ name: Embed type: module description: 'Provide a framework for various different types of embeds in WYSIWYG editors, common functionality, interfaces, and standards.' core: 8.x +dependencies: + - image diff --git a/src/Entity/EmbedButton.php b/src/Entity/EmbedButton.php index a2e0a55..a71825a 100644 --- a/src/Entity/EmbedButton.php +++ b/src/Entity/EmbedButton.php @@ -117,7 +117,8 @@ public function getIconFile() { */ public function getIconUrl() { if ($image = $this->getIconFile()) { - return $image->url(); + $style = \Drupal\image\Entity\ImageStyle::load('embed_button'); + return $style->buildUri($image->getFileUri()); } else { return file_create_url(drupal_get_path('module', 'embed') . '/js/plugins/drupalembed/embed.png'); diff --git a/src/Form/EmbedButtonForm.php b/src/Form/EmbedButtonForm.php index cfe86b3..45fa530 100644 --- a/src/Form/EmbedButtonForm.php +++ b/src/Form/EmbedButtonForm.php @@ -14,6 +14,7 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\ElementInfoManagerInterface; use Drupal\ckeditor\CKEditorPluginManager; use Drupal\embed\EmbedDisplay\EmbedDisplayManager; use Drupal\embed\EmbedType\EmbedTypeManager; @@ -59,6 +60,13 @@ class EmbedButtonForm extends EntityForm { */ protected $entityEmbedConfig; + /** + * The element info manager. + * + * @var \Drupal\Core\Render\ElementInfoManagerInterface + */ + protected $elementInfo; + /** * Constructs a new EmbedButtonForm. * @@ -73,12 +81,13 @@ class EmbedButtonForm extends EntityForm { * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. */ - public function __construct(EntityManagerInterface $entity_manager, EmbedTypeManager $embed_type_manager, EmbedDisplayManager $display_plugin_manager, CKEditorPluginManager $ckeditor_plugin_manager, ConfigFactoryInterface $config_factory) { + public function __construct(EntityManagerInterface $entity_manager, EmbedTypeManager $embed_type_manager, EmbedDisplayManager $display_plugin_manager, CKEditorPluginManager $ckeditor_plugin_manager, ConfigFactoryInterface $config_factory, ElementInfoManagerInterface $element_info) { $this->entityManager = $entity_manager; $this->typePluginManager = $embed_type_manager; $this->displayPluginManager = $display_plugin_manager; $this->ckeditorPluginManager = $ckeditor_plugin_manager; $this->entityEmbedConfig = $config_factory->get('embed.settings'); + $this->elementInfo = $element_info; } /** @@ -90,7 +99,8 @@ public static function create(ContainerInterface $container) { $container->get('plugin.manager.embed.type'), $container->get('plugin.manager.embed.display'), $container->get('plugin.manager.ckeditor.plugin'), - $container->get('config.factory') + $container->get('config.factory'), + $container->get('element_info') ); } @@ -155,12 +165,22 @@ public function form(array $form, FormStateInterface $form_state) { $form['icon_file'] = array( '#title' => $this->t('Button icon image'), '#type' => 'managed_file', - '#description' => $this->t("Image for the button to be shown in CKEditor toolbar. Leave empty to use the default Entity icon."), + '#description' => $this->t("Image for the button to be shown in CKEditor toolbar. Leave empty to use the default Entity icon. For best results upload a 32x32 or 16x16 image."), '#upload_location' => $upload_location, '#upload_validators' => array( 'file_validate_extensions' => array('gif png jpg jpeg'), - 'file_validate_image_resolution' => array('32x32', '16x16'), ), + '#multiple' => FALSE, + // Styling for nice image upload. + '#process' => array_merge($this->elementInfo->getInfo('managed_file')['#process'], array(array('\Drupal\image\Plugin\Field\FieldWidget\ImageWidget', 'process'))), + '#preview_image_style' => 'embed_button', + '#title_field' => 0, + '#title_field_required' => 0, + '#alt_field' => 0, + '#alt_field_required' => 0, + '#display_field' => 0, + '#description_field' => 0, + '#cardinality' => 1, ); if ($file = $embed_button->getIconFile()) { $form['icon_file']['#default_value'] = array('target_id' => $file->id());