Commit 0c5d5ef5 authored by Ankitha Shetty's avatar Ankitha Shetty Committed by Damien McKenna
Browse files

Issue #3171302 by ankithashetty, DamienMcKenna: \Drupal calls should be...

Issue #3171302 by ankithashetty, DamienMcKenna: \Drupal calls should be avoided in classes, use dependency injection instead.
parent 635b951d
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@ Metatag 8.x-1.x-dev, xxxx-xx-xx
#2994433 by DamienMcKenna, ChandeepKhosa, vishnukumar, Berdir, joakland,
  pdenooijer, benstjohn, muranod: Automatically parse URLs from image field
  tokens.
#3171302 by ankithashetty, DamienMcKenna: \Drupal calls should be avoided in
  classes, use dependency injection instead.


Metatag 8.x-1.14, 2020-08-11
+2 −2
Original line number Diff line number Diff line
@@ -76,8 +76,8 @@ class MetatagViewsEditForm extends FormBase {
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    // Get the parameters from request.
    $view_id = \Drupal::request()->get('view_id');
    $display_id = \Drupal::request()->get('display_id');
    $view_id = $this->getRequest()->get('view_id');
    $display_id = $this->getRequest()->get('display_id');

    // Get meta tags from the view entity.
    $metatags = [];
+5 −5
Original line number Diff line number Diff line
@@ -138,7 +138,7 @@ class MetatagViewsTranslationForm extends FormBase {
    $config_name = $this->view->getConfigDependencyName();
    $config_path = 'display.' . $this->displayId . '.display_options.display_extenders.metatag_display_extender.metatags';

    $configuration = \Drupal::service('config.factory')->get($config_name);
    $configuration = $this->configFactory()->get($config_name);
    $this->baseData = $configuration->getOriginal($config_path, FALSE);

    // Set the translation target language on the configuration factory.
@@ -146,7 +146,7 @@ class MetatagViewsTranslationForm extends FormBase {
    $this->languageManager->setConfigOverrideLanguage($this->language);

    // Read in translated values.
    $configuration = \Drupal::service('config.factory')->get($config_name);
    $configuration = $this->configFactory()->get($config_name);
    $translated_values = $configuration->get($config_path);

    // Set the configuration language back.
@@ -160,9 +160,9 @@ class MetatagViewsTranslationForm extends FormBase {
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    // Get the parameters from request.
    $this->viewId = \Drupal::request()->get('view_id');
    $this->displayId = \Drupal::request()->get('display_id');
    $langcode = \Drupal::request()->get('langcode');
    $this->viewId = $this->getRequest()->get('view_id');
    $this->displayId = $this->getRequest()->get('display_id');
    $langcode = $this->getRequest()->get('langcode');

    $this->view = $this->viewsManager->load($this->viewId);
    $this->language = $this->languageManager->getLanguage($langcode);
+85 −18
Original line number Diff line number Diff line
@@ -4,10 +4,16 @@ namespace Drupal\metatag\Form;

use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\metatag\MetatagManager;
use Drupal\metatag\MetatagManagerInterface;
use Drupal\metatag\MetatagTagPluginManager;
use Drupal\metatag\MetatagToken;
use Drupal\page_manager\Entity\PageVariant;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class MetatagDefaultsForm.
@@ -16,13 +22,82 @@ use Drupal\page_manager\Entity\PageVariant;
 */
class MetatagDefaultsForm extends EntityForm {

  /**
   * The entity type bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * The Metatag manager service.
   *
   * @var \Drupal\metatag\MetatagManagerInterface
   */
  protected $metatagManager;

  /**
   * The Metatag token service.
   *
   * @var \Drupal\metatag\MetatagToken
   */
  protected $metatagToken;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The Metatag tag plugin manager service.
   *
   * @var \Drupal\metatag\MetatagTagPluginManager
   */
  protected $metatagPluginManager;

  /**
   * Constructs a new MetatagDefaultsForm.
   *
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle service.
   * @param \Drupal\metatag\MetatagManagerInterface $metatag_manager
   *   The Metatag manager service.
   * @param \Drupal\metatag\MetatagToken $metatag_token
   *   The Metatag token service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   * @param \Drupal\metatag\MetatagTagPluginManager $metatag_plugin_manager
   *   The Metatag tag plugin manager service.
   */
  public function __construct(EntityTypeBundleInfoInterface $entity_type_bundle_info, MetatagManagerInterface $metatag_manager, MetatagToken $metatag_token, ModuleHandlerInterface $module_handler, MetatagTagPluginManager $metatag_plugin_manager) {
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
    $this->metatagManager = $metatag_manager;
    $this->metatagToken = $metatag_token;
    $this->moduleHandler = $module_handler;
    $this->metatagPluginManager = $metatag_plugin_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.bundle.info'),
      $container->get('metatag.manager'),
      $container->get('metatag.token'),
      $container->get('module_handler'),
      $container->get('plugin.manager.metatag.tag')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $metatag_defaults = $this->entity;
    $metatag_manager = \Drupal::service('metatag.manager');

    $form['#ajax_wrapper_id'] = 'metatag-defaults-form-ajax-wrapper';
    $ajax = [
@@ -87,11 +162,11 @@ class MetatagDefaultsForm extends EntityForm {
    $groups = !empty($entity_type_groups[$entity_type]) && !empty($entity_type_groups[$entity_type][$entity_bundle]) ? $entity_type_groups[$entity_type][$entity_bundle] : [];
    // Limit the form to requested groups, if any.
    if (!empty($groups)) {
      $form = $metatag_manager->form($values, $form, [$entity_type], $groups, NULL, TRUE);
      $form = $this->metatagManager->form($values, $form, [$entity_type], $groups, NULL, TRUE);
    }
    // Otherwise, display all groups.
    else {
      $form = $metatag_manager->form($values, $form, [], NULL, NULL, TRUE);
      $form = $this->metatagManager->form($values, $form, [], NULL, NULL, TRUE);
    }

    $form['status'] = [
@@ -164,19 +239,16 @@ class MetatagDefaultsForm extends EntityForm {
      $entity_bundle = isset($type_parts[1]) ? $type_parts[1] : NULL;

      // Get the entity label.
      $entity_type_manager = \Drupal::service('entity_type.manager');
      $entity_info = $entity_type_manager->getDefinitions();
      $entity_info = $this->entityTypeManager->getDefinitions();
      $entity_label = (string) $entity_info[$entity_type]->get('label');

      if (!is_null($entity_bundle)) {
        // Get the bundle label.
        $bundle_manager = \Drupal::service('entity_type.bundle.info');
        $bundle_info = $bundle_manager->getBundleInfo($entity_type);
        $bundle_info = $this->entityTypeBundleInfo->getBundleInfo($entity_type);
        if ($entity_type === 'page_variant') {
          // Check if page manager is enabled and try to load the page variant
          // so the label of the variant can be used.
          $moduleHandler = \Drupal::service('module_handler');
          if ($moduleHandler->moduleExists('metatag_page_manager')) {
          if ($this->moduleHandler->moduleExists('metatag_page_manager')) {
            $page_variant = PageVariant::load($entity_bundle);
            $page = $page_variant->getPage();
            if ($page_variant) {
@@ -194,14 +266,13 @@ class MetatagDefaultsForm extends EntityForm {
    }

    // Set tags within the Metatag entity.
    $tag_manager = \Drupal::service('plugin.manager.metatag.tag');
    $tags = $tag_manager->getDefinitions();
    $tags = $this->metatagPluginManager->getDefinitions();
    $tag_values = [];
    foreach ($tags as $tag_id => $tag_definition) {
      if ($form_state->hasValue($tag_id)) {
        // Some plugins need to process form input before storing it. Hence, we
        // set it and then get it.
        $tag = $tag_manager->createInstance($tag_id);
        $tag = $this->metatagPluginManager->createInstance($tag_id);
        $tag->setValue($form_state->getValue($tag_id));
        if (!empty($tag->value())) {
          $tag_values[$tag_id] = $tag->value();
@@ -236,17 +307,13 @@ class MetatagDefaultsForm extends EntityForm {
  protected function getAvailableBundles() {
    $options = [];
    $entity_types = static::getSupportedEntityTypes();
    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
    $entity_type_manager = \Drupal::service('entity_type.manager');
    /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
    $bundle_info = \Drupal::service('entity_type.bundle.info');
    $metatags_defaults_manager = $entity_type_manager->getStorage('metatag_defaults');
    $metatags_defaults_manager = $this->entityTypeManager->getStorage('metatag_defaults');
    foreach ($entity_types as $entity_type => $entity_label) {
      if (empty($metatags_defaults_manager->load($entity_type))) {
        $options[$entity_label][$entity_type] = "$entity_label (Default)";
      }

      $bundles = $bundle_info->getBundleInfo($entity_type);
      $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type);
      foreach ($bundles as $bundle_id => $bundle_metadata) {
        $metatag_defaults_id = $entity_type . '__' . $bundle_id;