Skip to content
Snippets Groups Projects

#3417227 - Add di

2 files
+ 141
3
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 37
3
@@ -2,10 +2,13 @@
namespace Drupal\block_attributes\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Serialization\Yaml;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Configuration form for the block attributes.
@@ -14,6 +17,37 @@ use Drupal\Component\Serialization\Yaml;
*/
class ConfigForm extends ConfigFormBase {
/**
* Returns the module_handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a ConfigForm form.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* Interface for classes that manage a set of enabled modules.
*/
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('module_handler')
);
}
/**
* {@inheritdoc}
*/
@@ -38,12 +72,12 @@ class ConfigForm extends ConfigFormBase {
$conf = $config->get();
$config_text = Yaml::encode($conf);
if (!\Drupal::moduleHandler()->moduleExists('yaml_editor')) {
if (!$this->moduleHandler->moduleExists('yaml_editor')) {
$message = $this->t('It is recommended to install the <a href="@yaml-editor">YAML Editor</a> module for easier editing.', [
'@yaml-editor' => 'https://www.drupal.org/project/yaml_editor',
]);
\Drupal::messenger()->addWarning($message, 'warning');
$this->messenger()->addWarning($message, 'warning');
}
$form['config'] = [
@@ -56,7 +90,7 @@ class ConfigForm extends ConfigFormBase {
];
// Use module's YAML config file for example structure.
$module_path = \Drupal::moduleHandler()->getModule('block_attributes')->getPath();
$module_path = $this->moduleHandler->getModule('block_attributes')->getPath();
$yml_text = file_get_contents($module_path . '/config/install/block_attributes.config.yml');
$form['example'] = [
Loading