Skip to content
Snippets Groups Projects
Commit 1a2179f7 authored by samit.khulve1's avatar samit.khulve1
Browse files

3344389: using DI for entity.repository service

parent ca4e4a81
No related merge requests found
Pipeline #182987 passed
Pipeline: drupal-3344389

#182989

    ......@@ -44,7 +44,7 @@ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInter
    * {@inheritdoc}
    */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container->get('element_info'));
    return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container->get('element_info'), $container->get('entity.repository'));
    }
    /**
    ......
    ......@@ -2,6 +2,7 @@
    namespace Drupal\image\Plugin\Field\FieldWidget;
    use Drupal\Core\Entity\EntityRepositoryInterface;
    use Drupal\Core\Field\Attribute\FieldWidget;
    use Drupal\Core\Field\FieldDefinitionInterface;
    use Drupal\Core\Field\FieldItemListInterface;
    ......@@ -30,6 +31,13 @@ class ImageWidget extends FileWidget {
    */
    protected $imageFactory;
    /**
    * The entity repository.
    *
    * @var \Drupal\Core\Entity\EntityRepositoryInterface
    */
    protected $entityRepository;
    /**
    * Constructs an ImageWidget object.
    *
    ......@@ -45,11 +53,14 @@ class ImageWidget extends FileWidget {
    * Any third party settings.
    * @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info
    * The element info manager service.
    * @param \Drupal\Core\Image\ImageFactory $image_factory
    * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
    * The entity repository.
    * @param \Drupal\Core\Image\ImageFactory|null $image_factory
    * The image factory service.
    */
    public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, ElementInfoManagerInterface $element_info, ImageFactory $image_factory = NULL) {
    public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, ElementInfoManagerInterface $element_info, EntityRepositoryInterface $entity_repository, ImageFactory $image_factory = NULL) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $element_info);
    $this->entityRepository = $entity_repository;
    $this->imageFactory = $image_factory ?: \Drupal::service('image.factory');
    }
    ......@@ -179,7 +190,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
    $default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image');
    }
    // Convert the stored UUID into a file ID.
    if (!empty($default_image['uuid']) && $entity = \Drupal::service('entity.repository')->loadEntityByUuid('file', $default_image['uuid'])) {
    if (!empty($default_image['uuid']) && $entity = $this->entityRepository->loadEntityByUuid('file', $default_image['uuid'])) {
    $default_image['fid'] = $entity->id();
    }
    $element['#default_image'] = !empty($default_image['fid']) ? $default_image : [];
    ......
    ......@@ -3,6 +3,7 @@
    namespace Drupal\taxonomy\Plugin\views\filter;
    use Drupal\Core\Entity\Element\EntityAutocomplete;
    use Drupal\Core\Entity\EntityRepositoryInterface;
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\Core\Session\AccountInterface;
    use Drupal\taxonomy\Entity\Term;
    ......@@ -51,6 +52,13 @@ class TaxonomyIndexTid extends ManyToOne {
    */
    protected $currentUser;
    /**
    * The entity repository.
    *
    * @var \Drupal\Core\Entity\EntityRepositoryInterface
    */
    protected $entityRepository;
    /**
    * Constructs a TaxonomyIndexTid object.
    *
    ......@@ -66,12 +74,15 @@ class TaxonomyIndexTid extends ManyToOne {
    * The term storage.
    * @param \Drupal\Core\Session\AccountInterface $current_user
    * The current user.
    * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
    * The entity repository.
    */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, VocabularyStorageInterface $vocabulary_storage, TermStorageInterface $term_storage, AccountInterface $current_user) {
    public function __construct(array $configuration, $plugin_id, $plugin_definition, VocabularyStorageInterface $vocabulary_storage, TermStorageInterface $term_storage, AccountInterface $current_user, EntityRepositoryInterface $entity_repository) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->vocabularyStorage = $vocabulary_storage;
    $this->termStorage = $term_storage;
    $this->currentUser = $current_user;
    $this->entityRepository = $entity_repository;
    }
    /**
    ......@@ -84,7 +95,8 @@ public static function create(ContainerInterface $container, array $configuratio
    $plugin_definition,
    $container->get('entity_type.manager')->getStorage('taxonomy_vocabulary'),
    $container->get('entity_type.manager')->getStorage('taxonomy_term'),
    $container->get('current_user')
    $container->get('current_user'),
    $container->get('entity.repository')
    );
    }
    ......@@ -202,7 +214,7 @@ protected function valueForm(&$form, FormStateInterface $form_state) {
    continue;
    }
    $choice = new \stdClass();
    $choice->option = [$term->id() => str_repeat('-', $term->depth) . \Drupal::service('entity.repository')->getTranslationFromContext($term)->label()];
    $choice->option = [$term->id() => str_repeat('-', $term->depth) . $this->entityRepository->getTranslationFromContext($term)->label()];
    $options[] = $choice;
    }
    }
    ......@@ -224,7 +236,7 @@ protected function valueForm(&$form, FormStateInterface $form_state) {
    }
    $terms = Term::loadMultiple($query->execute());
    foreach ($terms as $term) {
    $options[$term->id()] = \Drupal::service('entity.repository')->getTranslationFromContext($term)->label();
    $options[$term->id()] = $this->entityRepository->getTranslationFromContext($term)->label();
    }
    }
    ......@@ -395,7 +407,7 @@ public function adminSummary() {
    $this->value = array_filter($this->value);
    $terms = Term::loadMultiple($this->value);
    foreach ($terms as $term) {
    $this->valueOptions[$term->id()] = \Drupal::service('entity.repository')->getTranslationFromContext($term)->label();
    $this->valueOptions[$term->id()] = $this->entityRepository->getTranslationFromContext($term)->label();
    }
    }
    return parent::adminSummary();
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please to comment