Commit bdfba65b authored by renatog's avatar renatog Committed by renatog
Browse files

Issue #3269576 by RenatoG: Add dependency injection on BlockClassController,...

Issue #3269576 by RenatoG: Add dependency injection on BlockClassController, BlockClassBulkOperationsForm and BlockClassSettingsForm
parent 89efafd4
Loading
Loading
Loading
Loading
+4 −2
Changes for src/Controller/BlockClassController.php: 4 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -172,9 +172,11 @@ class BlockClassController extends ControllerBase {

    $block_classes_stored = [];

    $config = $this->configFactory->getEditable('block_class.settings');

    // Get config object.
    if (!empty(\Drupal::config('block_class.settings')->get('block_classes_stored'))) {
      $block_classes_stored = \Drupal::config('block_class.settings')->get('block_classes_stored');
    if (!empty($config->get('block_classes_stored'))) {
      $block_classes_stored = $config->get('block_classes_stored');
    }

    // Get the array.
+68 −2
Changes for src/Form/BlockClassBulkOperationsForm.php: 68 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -7,6 +7,12 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Session;
use Drupal\Core\Extension\ExtensionList;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;

/**
 * Form for Block Class Settings.
@@ -15,6 +21,66 @@ class BlockClassBulkOperationsForm extends ConfigFormBase {

  use StringTranslationTrait;

  /**
   * The messenger.
   *
   * @var \Symfony\Component\HttpFoundation\Session\Session
   */
  protected $session;


  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The extension list module.
   *
   * @var \Drupal\Core\Extension\ExtensionList
   */
  protected $extensionListModule;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Creates the construct.
   */
  public function __construct(RouteMatchInterface $route_match, ExtensionList $extension_list_module, ConfigFactoryInterface $config_factory, MessengerInterface $messenger, Session $session) {
    $this->routeMatch = $route_match;
    $this->extensionListModule = $extension_list_module;
    $this->configFactory = $config_factory;
    $this->messenger = $messenger;
    $this->session = $session;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('current_route_match'),
      $container->get('extension.list.module'),
      $container->get('config.factory'),
      $container->get('messenger'),
      $container->get('session')
    );
  }

  /**
   * {@inheritdoc}
   */
@@ -146,8 +212,8 @@ class BlockClassBulkOperationsForm extends ConfigFormBase {

    // Set session to confirm bulk operation. This session will be used on
    // confirmation page. So we'll show the confirmation only for users with
    // this session. @Todo: We'll implement the DI in a separated ticket.
    \Drupal::service('session')->set('block_class_confirm_bulk_operation', $operation);
    // this session.
    $this->session->set('block_class_confirm_bulk_operation', $operation);

    // Get path confirmation bulk operation.
    $path_confirm_bulk_operation = Url::fromRoute('block_class.confirm_bulk_operation', $parameters)->toString();
+58 −2
Changes for src/Form/BlockClassSettingsForm.php: 58 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,11 @@ namespace Drupal\block_class\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Extension\ExtensionList;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;

/**
 * Form for Block Class Settings.
@@ -13,6 +18,56 @@ class BlockClassSettingsForm extends ConfigFormBase {

  use StringTranslationTrait;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The extension list module.
   *
   * @var \Drupal\Core\Extension\ExtensionList
   */
  protected $extensionListModule;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Creates the construct.
   */
  public function __construct(RouteMatchInterface $route_match, ExtensionList $extension_list_module, ConfigFactoryInterface $config_factory, MessengerInterface $messenger) {
    $this->routeMatch = $route_match;
    $this->extensionListModule = $extension_list_module;
    $this->configFactory = $config_factory;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('current_route_match'),
      $container->get('extension.list.module'),
      $container->get('config.factory'),
      $container->get('messenger')
    );
  }

  /**
   * {@inheritdoc}
   */
@@ -148,7 +203,8 @@ class BlockClassSettingsForm extends ConfigFormBase {

    if (($config->get('default_case') != $default_case) && ($default_case == 'uppercase' || $default_case == 'lowercase')) {

      // Put warning messages updating case type only when is updating to a different one.
      // Put warning messages updating case type only when is updating to a
      // different one.
      switch ($default_case) {

        case 'uppercase':
@@ -166,7 +222,7 @@ class BlockClassSettingsForm extends ConfigFormBase {
      }

      // Show the message on submit.
      \Drupal::messenger()->addWarning($warning_message_case);
      $this->messenger->addWarning($warning_message_case);
    }

    // Set the values.