diff --git a/crop.info.yml b/crop.info.yml index ee038e8..354072e 100644 --- a/crop.info.yml +++ b/crop.info.yml @@ -4,8 +4,8 @@ description: 'Provides storage and API for image crops.' package: Media type: module dependencies: - - image - - user + - drupal:image + - drupal:user # Information added by Drupal.org packaging script on 2017-10-26 version: '8.x-2.0-beta1' diff --git a/crop.module b/crop.module index ff3ec57..8f57064 100644 --- a/crop.module +++ b/crop.module @@ -12,6 +12,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\StreamWrapper\PublicStream; use Drupal\crop\Entity\Crop; use Drupal\image\Entity\ImageStyle; +use Drupal\media\MediaSourceInterface; use Drupal\media\MediaTypeInterface; use Drupal\file\FileInterface; @@ -39,16 +40,19 @@ function template_preprocess_crop_crop_summary(&$variables) { } /** - * Implements hook_form_BASE_ID_alter(). + * Implements hook_form_FORM_ID_alter(). * - * Adds crop configuraiton fields to media bundle form. + * Adds crop configuration fields to media form. */ -function crop_form_media_type_form_alter(&$form, FormStateInterface $form_state, $form_id) { - /** @var \Drupal\media\MediaTypeInterface $bundle */ - $bundle = $form_state->getStorage()['type']; +function crop_form_media_type_edit_form_alter(array &$form, FormStateInterface $form_state, $form_id) { + /** @var \Drupal\media\Entity\MediaType $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(); } @@ -58,6 +62,7 @@ function crop_form_media_type_form_alter(&$form, FormStateInterface $form_state, $form['crop'] = [ '#type' => 'fieldset', '#title' => t('Crop configuration'), + '#group' => 'source_dependent', ]; if (empty($options)) { @@ -76,21 +81,22 @@ function crop_form_media_type_form_alter(&$form, FormStateInterface $form_state, $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' => MediaSourceInterface::METADATA_FIELD_EMPTY, '#description' => t('Select field that stores image which needs to be cropped.'), ]; - } /** - * Entity builder for Media bundle. + * Entity builder for Media type. * - * Adds third party settings to Media bundle config entity. + * Adds third party settings to Media type config entity. * - * @see crop_form_media_type_form_alter() + * @see crop_form_media_type_edit_form_alter() */ -function crop_media_type_form_builder($entity_type, MediaTypeInterface $bundle, &$form, FormStateInterface $form_state) { +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')); } diff --git a/modules/crop_media_entity/crop_media_entity.info.yml b/modules/crop_media_entity/crop_media_entity.info.yml deleted file mode 100644 index 2f2f400..0000000 --- a/modules/crop_media_entity/crop_media_entity.info.yml +++ /dev/null @@ -1,14 +0,0 @@ -name: Media entity crop -description: 'Provides Media entity integration for Crop API.' -# core: 8.x -package: Media -type: module -dependencies: - - crop - - drupal:media (>= 8.4) - -# Information added by Drupal.org packaging script on 2017-10-26 -version: '8.x-2.0-beta1' -core: '8.x' -project: 'crop' -datestamp: 1509015255 diff --git a/modules/crop_media_entity/crop_media_entity.module b/modules/crop_media_entity/crop_media_entity.module deleted file mode 100644 index f960ae8..0000000 --- a/modules/crop_media_entity/crop_media_entity.module +++ /dev/null @@ -1,6 +0,0 @@ - $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' => '

', - '#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' => '

', ]; 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); diff --git a/src/Form/CropTypeForm.php b/src/Form/CropTypeForm.php index 6874a43..9d89df6 100644 --- a/src/Form/CropTypeForm.php +++ b/src/Form/CropTypeForm.php @@ -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); } diff --git a/modules/crop_media_entity/src/Plugin/Crop/EntityProvider/MediaEntity.php b/src/Plugin/Crop/EntityProvider/Media.php similarity index 78% rename from modules/crop_media_entity/src/Plugin/Crop/EntityProvider/MediaEntity.php rename to src/Plugin/Crop/EntityProvider/Media.php index 92b275a..f3d6127 100644 --- a/modules/crop_media_entity/src/Plugin/Crop/EntityProvider/MediaEntity.php +++ b/src/Plugin/Crop/EntityProvider/Media.php @@ -1,6 +1,6 @@ entityTypeManager->getStorage('media_type')->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; diff --git a/src/Plugin/ImageEffect/CropEffect.php b/src/Plugin/ImageEffect/CropEffect.php index dadb2af..dfdc72a 100644 --- a/src/Plugin/ImageEffect/CropEffect.php +++ b/src/Plugin/ImageEffect/CropEffect.php @@ -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