Commit bb8caee4 authored by woprrr's avatar woprrr Committed by woprrr

Issue #2918441 by woprrr, balsama, phenaproxima: Make Crop API compatible with core Media

parent 236a50ed
......@@ -7,12 +7,14 @@
* Provides storage and API for image crops.
*/
use Drupal\Component\Utility\UrlHelper;
use \Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\crop\Entity\Crop;
use Drupal\image\Entity\ImageStyle;
use \Drupal\media_entity\MediaBundleInterface;
use \Drupal\file\FileInterface;
use Drupal\media\Entity\MediaType;
use Drupal\media\MediaTypeInterface;
use Drupal\media_entity\MediaBundleInterface;
use Drupal\file\FileInterface;
/**
* Implements hook_theme().
......@@ -37,26 +39,55 @@ function template_preprocess_crop_crop_summary(&$variables) {
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds crop configuration fields to media type form.
*/
function crop_form_media_type_edit_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
_crop_media_provider_form($form, $form_state);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds crop configuration fields to media bundle form.
*/
function crop_form_media_bundle_edit_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\media_entity\MediaBundleInterface $bundle */
$bundle = $form['#entity'];
_crop_media_provider_form($form, $form_state);
}
/**
* Helper function to avoid uneeded code duplication.
*
* @todo Delete this and media entity fallback when media is stable.
*/
function _crop_media_provider_form(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Config\Entity\ConfigEntityBundleBase $entity_type */
$entity_type = $form_state->getFormObject()->getEntity();
$options = [];
$allowed_field_types = ['file', 'image'];
foreach (\Drupal::service('entity_field.manager')->getFieldDefinitions('media', $bundle->id()) as $field_name => $field) {
/** @var \Drupal\Core\Field\FieldDefinitionInterface[] $fields */
$fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('media', $entity_type->id());
foreach ($fields as $field_name => $field) {
if (in_array($field->getType(), $allowed_field_types) && !$field->getFieldStorageDefinition()->isBaseField()) {
$options[$field_name] = $field->getLabel();
}
}
$form['#entity_builders'][] = 'crop_media_bundle_form_builder';
// Maintain compatibility with Media Entity.
if ($entity_type instanceof MediaType) {
$form['#entity_builders'][] = 'crop_media_type_form_builder';
}
else {
$form['#entity_builders'][] = 'crop_media_bundle_form_builder';
}
$form['crop'] = [
'#type' => 'fieldset',
'#title' => t('Crop configuration'),
'#group' => 'source_dependent',
];
if (empty($options)) {
......@@ -75,13 +106,14 @@ function crop_form_media_bundle_edit_form_alter(array &$form, FormStateInterface
$form['crop']['image_field'] = [
'#type' => 'select',
'#title' => t('Image field'),
'#default_value' => $bundle->getThirdPartySetting('crop', 'image_field'),
'#default_value' => $entity_type->getThirdPartySetting('crop', 'image_field'),
'#options' => $options,
'#empty_option' => t('- Skip field -'),
'#empty_value' => '_none',
'#description' => t('Select field that stores image which needs to be cropped.'),
];
return $form;
}
/**
......@@ -95,6 +127,17 @@ function crop_media_bundle_form_builder($entity_type, MediaBundleInterface $bund
$bundle->setThirdPartySetting('crop', 'image_field', $form_state->getValue('image_field'));
}
/**
* Entity builder for Media type.
*
* Adds third party settings to Media type config entity.
*
* @see crop_form_media_type_edit_form_alter()
*/
function crop_media_type_form_builder($entity_type, MediaTypeInterface $bundle, array &$form, FormStateInterface $form_state) {
$bundle->setThirdPartySetting('crop', 'image_field', $form_state->getValue('image_field'));
}
/**
* Implements hook_ENTITY_TYPE_delete().
*
......
name: Media entity crop
description: 'Provides Media entity integration for Crop API.'
core: 8.x
package: Media
type: module
dependencies:
- crop
- media_entity
<?php
/**
* @file
* The Media entity integration for Crop API.
*/
......@@ -55,7 +55,7 @@ class CropTypeDeleteForm extends EntityConfirmFormBase {
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to delete the crop type %type?', array('%type' => $this->entity->label()));
return t('Are you sure you want to delete the crop type %type?', ['%type' => $this->entity->label()]);
}
/**
......@@ -84,7 +84,7 @@ class CropTypeDeleteForm extends EntityConfirmFormBase {
$form['#title'] = $this->getQuestion();
$form['description'] = [
'#prefix' => '<p>',
'#markup' => $this->translation->formatPlural($count, '%type is used by 1 piece of content on your site. You can not remove this content type until you have removed all of the %type content.', '%type is used by @count pieces of content on your site. You may not remove %type until you have removed all of the %type content.', array('%type' => $this->entity->label())),
'#markup' => $this->translation->formatPlural($count, '%type is used by 1 piece of content on your site. You can not remove this content type until you have removed all of the %type content.', '%type is used by @count pieces of content on your site. You may not remove %type until you have removed all of the %type content.', ['%type' => $this->entity->label()]),
'#suffix' => '</p>',
];
return $form;
......@@ -98,7 +98,7 @@ class CropTypeDeleteForm extends EntityConfirmFormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$t_args = array('%name' => $this->entity->label());
$t_args = ['%name' => $this->entity->label()];
drupal_set_message($this->t('The crop type %name has been deleted.', $t_args));
$this->logger('crop')->notice('Deleted crop type %name.', $t_args);
......
......@@ -5,7 +5,6 @@ namespace Drupal\crop\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\Validator\ConstraintViolationListInterface;
/**
......@@ -22,7 +21,7 @@ class CropTypeForm extends EntityForm {
$type = $this->entity;
$form['#title'] = $this->operation == 'add' ? $this->t('Add crop type')
:
$this->t('Edit %label crop type', array('%label' => $type->label()));
$this->t('Edit %label crop type', ['%label' => $type->label()]);
$form['label'] = [
'#title' => $this->t('Name'),
......@@ -169,14 +168,14 @@ class CropTypeForm extends EntityForm {
$status = $type->save();
$t_args = array('%name' => $type->label());
$t_args = ['%name' => $type->label()];
if ($status == SAVED_UPDATED) {
drupal_set_message($this->t('The crop type %name has been updated.', $t_args));
}
elseif ($status == SAVED_NEW) {
drupal_set_message($this->t('The crop type %name has been added.', $t_args));
$context = array_merge($t_args, array('link' => Link::createFromRoute($this->t('View'), 'crop.overview_types')->toString()));
$context = array_merge($t_args, ['link' => Link::createFromRoute($this->t('View'), 'crop.overview_types')->toString()]);
$this->logger('crop')->notice('Added crop type %name.', $context);
}
......
<?php
namespace Drupal\crop_media_entity\Plugin\Crop\EntityProvider;
namespace Drupal\crop\Plugin\Crop\EntityProvider;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
......@@ -9,15 +9,15 @@ use Drupal\crop\EntityProviderBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Media entity crop integration.
* Media crop integration.
*
* @CropEntityProvider(
* entity_type = "media",
* label = @Translation("Media"),
* description = @Translation("Provides crop integration for media entity.")
* description = @Translation("Provides crop integration for Media.")
* )
*/
class MediaEntity extends EntityProviderBase implements ContainerFactoryPluginInterface {
class Media extends EntityProviderBase implements ContainerFactoryPluginInterface {
/**
* Entity type manager service.
......@@ -27,7 +27,7 @@ class MediaEntity extends EntityProviderBase implements ContainerFactoryPluginIn
protected $entityTypeManager;
/**
* Constructs media entity integration plugin.
* Constructs media integration plugin.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
......@@ -59,9 +59,12 @@ class MediaEntity extends EntityProviderBase implements ContainerFactoryPluginIn
* {@inheritdoc}
*/
public function uri(EntityInterface $entity) {
/** @var \Drupal\media_entity\MediaBundleInterface $bundle */
$bundle = $this->entityTypeManager->getStorage('media_bundle')->load($entity->bundle());
$image_field = $bundle->getThirdPartySetting('crop', 'image_field');
$bundle_entity_type = $entity->getEntityType()->getBundleEntityType();
/** @var \Drupal\Core\Config\Entity\ConfigEntityBase $entity_type */
$entity_type = $this->entityTypeManager->getStorage($bundle_entity_type)->load($entity->bundle());
$image_field = $entity_type->getThirdPartySetting('crop', 'image_field');
if ($entity->{$image_field}->first()->isEmpty()) {
return FALSE;
......
......@@ -160,7 +160,7 @@ class CropEffect extends ConfigurableImageEffectBase implements ContainerFactory
/**
* Gets crop entity for the image.
*
* @param ImageInterface $image
* @param \Drupal\Core\Image\ImageInterface $image
* Image object.
*
* @return \Drupal\Core\Entity\EntityInterface|\Drupal\crop\CropInterface|false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment