Skip to content

Commit 0fe1993

Browse files
GueGuerreiroenzolutions
authored andcommitted
Removes constructor override on service object generators. (#4174)
* Issue #4169 - generate:controller * Issue #4169 - generate:form:config * Issue #4169 - generate:form * Smallfix. Adds spacing between Twig `use_class_services` block declarations. * Issue #4169 - generate:plugin:block * Issue #4169 - generate:plugin:mail * Issue #4169 - generate:plugin:skeleton This one has a lot of coding standard errors, as well as a malformed namespace. I have not attempted to fix those, as they are out of scope for this issue, although I fixed them for the create() function, since that's directly related to what I'm changing. * Issue #4169 - generate:plugin:rest:resource This one has some coding standard errors. I have not attempted to fix those, as they are out of scope for this issue, although I fixed them for the create() function and the currentUser property definition, since that's directly related to what I'm changing. * Issue #4169 - generate:plugin:condition * Issue #4169 - generate:plugin:imageformatter This one has some coding standard errors. I have not attempted to fix those, as they are out of scope for this issue, although I fixed them for the create() function and the property definitions, since that's directly related to what I'm changing. * Issue #4169 - generate:entity:content
1 parent ee6ad4a commit 0fe1993

14 files changed

+110
-411
lines changed

templates/module/src/Controller/controller.php.twig

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,25 @@ use Drupal\Core\Controller\ControllerBase;
1414
use Symfony\Component\DependencyInjection\ContainerInterface;
1515
{% endif %}
1616
{% endblock %}
17+
18+
{% block use_class_services %}
19+
{% endblock %}
20+
1721
{% block class_declaration %}
1822
/**
1923
* Class {{ class_name }}.
2024
*/
2125
class {{ class_name }} extends ControllerBase {% endblock %}
22-
{% block class_construct %}
23-
{% if services is not empty %}
2426

25-
/**
26-
* Constructs a new {{ class_name }} object.
27-
*/
28-
public function __construct({{ servicesAsParameters(services)|join(', ') }}) {
29-
{{ serviceClassInitialization(services) }}
30-
}
31-
{% endif %}
32-
{% endblock %}
3327
{% block class_create %}
3428
{% if services is not empty %}
35-
3629
/**
3730
* {@inheritdoc}
3831
*/
3932
public static function create(ContainerInterface $container) {
40-
return new static(
41-
{{ serviceClassInjection(services) }}
42-
);
33+
$instance = parent::create($container);
34+
{{ serviceClassInjectionNoOverride(services) }}
35+
return $instance;
4336
}
4437

4538
{% endif %}

templates/module/src/Controller/entity-controller.php.twig

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ namespace Drupal\{{ module }}\Controller;
1111
{% block use_class %}
1212
use Drupal\Component\Utility\Xss;
1313
use Drupal\Core\Controller\ControllerBase;
14-
use Drupal\Core\Datetime\DateFormatter;
1514
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
16-
use Drupal\Core\Render\Renderer;
1715
use Drupal\Core\Url;
1816
use Drupal\{{ module }}\Entity\{{ entity_class }}Interface;
1917
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -26,7 +24,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
2624
*/
2725
class {{ entity_class }}Controller extends ControllerBase implements ContainerInjectionInterface {% endblock %}
2826
{% block class_methods %}
29-
3027
/**
3128
* The date formatter.
3229
*
@@ -41,27 +38,14 @@ class {{ entity_class }}Controller extends ControllerBase implements ContainerIn
4138
*/
4239
protected $renderer;
4340

44-
/**
45-
* Constructs a new {{ entity_class }}Controller.
46-
*
47-
* @param \Drupal\Core\Datetime\DateFormatter $date_formatter
48-
* The date formatter.
49-
* @param \Drupal\Core\Render\Renderer $renderer
50-
* The renderer.
51-
*/
52-
public function __construct(DateFormatter $date_formatter, Renderer $renderer) {
53-
$this->dateFormatter = $date_formatter;
54-
$this->renderer = $renderer;
55-
}
56-
5741
/**
5842
* {@inheritdoc}
5943
*/
6044
public static function create(ContainerInterface $container) {
61-
return new static(
62-
$container->get('date.formatter'),
63-
$container->get('renderer')
64-
);
45+
$instance = parent::create($container);
46+
$instance->dateFormatter = $container->get('date.formatter');
47+
$instance->renderer = $container->get('renderer');
48+
return $instance;
6549
}
6650

6751
/**

templates/module/src/Entity/Form/entity-content-revision-delete.php.twig

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace Drupal\{{module}}\Form;
99
{% endblock %}
1010

1111
{% block use_class %}
12-
use Drupal\Core\Database\Connection;
13-
use Drupal\Core\Entity\EntityStorageInterface;
1412
use Drupal\Core\Form\ConfirmFormBase;
1513
use Drupal\Core\Form\FormStateInterface;
1614
use Drupal\Core\Url;
@@ -25,7 +23,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
2523
*/
2624
class {{ entity_class }}RevisionDeleteForm extends ConfirmFormBase {% endblock %}
2725
{% block class_methods %}
28-
2926
/**
3027
* The {{ label }} revision.
3128
*
@@ -38,7 +35,7 @@ class {{ entity_class }}RevisionDeleteForm extends ConfirmFormBase {% endblock %
3835
*
3936
* @var \Drupal\Core\Entity\EntityStorageInterface
4037
*/
41-
protected ${{ entity_class }}Storage;
38+
protected ${{ entity_class[:1]|lower ~ entity_class[1:] }}Storage;
4239

4340
/**
4441
* The database connection.
@@ -47,28 +44,14 @@ class {{ entity_class }}RevisionDeleteForm extends ConfirmFormBase {% endblock %
4744
*/
4845
protected $connection;
4946

50-
/**
51-
* Constructs a new {{ entity_class }}RevisionDeleteForm.
52-
*
53-
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
54-
* The entity storage.
55-
* @param \Drupal\Core\Database\Connection $connection
56-
* The database connection.
57-
*/
58-
public function __construct(EntityStorageInterface $entity_storage, Connection $connection) {
59-
$this->{{ entity_class }}Storage = $entity_storage;
60-
$this->connection = $connection;
61-
}
62-
6347
/**
6448
* {@inheritdoc}
6549
*/
6650
public static function create(ContainerInterface $container) {
67-
$entity_type_manager = $container->get('entity_type.manager');
68-
return new static(
69-
$entity_type_manager->getStorage('{{ entity_name }}'),
70-
$container->get('database')
71-
);
51+
$instance = parent::create($container);
52+
$instance->{{ entity_class[:1]|lower ~ entity_class[1:] }}Storage = $container->get('entity_type.manager')->getStorage('{{ entity_name }}');
53+
$instance->connection = $container->get('database');
54+
return $instance;
7255
}
7356

7457
/**

templates/module/src/Entity/Form/entity-content-revision-revert-translation.php.twig

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ namespace Drupal\{{module}}\Form;
99
{% endblock %}
1010

1111
{% block use_class %}
12-
use Drupal\Core\Datetime\DateFormatterInterface;
13-
use Drupal\Core\Entity\EntityStorageInterface;
1412
use Drupal\Core\Form\FormStateInterface;
15-
use Drupal\Core\Language\LanguageManagerInterface;
1613
use Drupal\{{module}}\Entity\{{ entity_class }}Interface;
1714
use Symfony\Component\DependencyInjection\ContainerInterface;
1815
{% endblock %}
@@ -25,7 +22,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
2522
*/
2623
class {{ entity_class }}RevisionRevertTranslationForm extends {{ entity_class }}RevisionRevertForm {% endblock %}
2724
{% block class_methods %}
28-
2925
/**
3026
* The language to be reverted.
3127
*
@@ -40,30 +36,13 @@ class {{ entity_class }}RevisionRevertTranslationForm extends {{ entity_class }}
4036
*/
4137
protected $languageManager;
4238

43-
/**
44-
* Constructs a new {{ entity_class }}RevisionRevertTranslationForm.
45-
*
46-
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
47-
* The {{ label }} storage.
48-
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
49-
* The date formatter service.
50-
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
51-
* The language manager.
52-
*/
53-
public function __construct(EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter, LanguageManagerInterface $language_manager) {
54-
parent::__construct($entity_storage, $date_formatter);
55-
$this->languageManager = $language_manager;
56-
}
57-
5839
/**
5940
* {@inheritdoc}
6041
*/
6142
public static function create(ContainerInterface $container) {
62-
return new static(
63-
$container->get('entity_type.manager')->getStorage('{{ entity_name }}'),
64-
$container->get('date.formatter'),
65-
$container->get('language_manager')
66-
);
43+
$instance = parent::create($container);
44+
$instance->languageManager = $container->get('language_manager');
45+
return $instance;
6746
}
6847

6948
/**

templates/module/src/Entity/Form/entity-content-revision-revert.php.twig

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace Drupal\{{module}}\Form;
99
{% endblock %}
1010

1111
{% block use_class %}
12-
use Drupal\Core\Datetime\DateFormatterInterface;
13-
use Drupal\Core\Entity\EntityStorageInterface;
1412
use Drupal\Core\Form\ConfirmFormBase;
1513
use Drupal\Core\Form\FormStateInterface;
1614
use Drupal\Core\Url;
@@ -26,7 +24,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
2624
*/
2725
class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %}
2826
{% block class_methods %}
29-
3027
/**
3128
* The {{ label }} revision.
3229
*
@@ -39,7 +36,7 @@ class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %
3936
*
4037
* @var \Drupal\Core\Entity\EntityStorageInterface
4138
*/
42-
protected ${{ entity_class }}Storage;
39+
protected ${{ entity_class[:1]|lower ~ entity_class[1:] }}Storage;
4340

4441
/**
4542
* The date formatter service.
@@ -48,27 +45,14 @@ class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %
4845
*/
4946
protected $dateFormatter;
5047

51-
/**
52-
* Constructs a new {{ entity_class }}RevisionRevertForm.
53-
*
54-
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
55-
* The {{ label }} storage.
56-
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
57-
* The date formatter service.
58-
*/
59-
public function __construct(EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter) {
60-
$this->{{ entity_class }}Storage = $entity_storage;
61-
$this->dateFormatter = $date_formatter;
62-
}
63-
6448
/**
6549
* {@inheritdoc}
6650
*/
6751
public static function create(ContainerInterface $container) {
68-
return new static(
69-
$container->get('entity_type.manager')->getStorage('{{ entity_name }}'),
70-
$container->get('date.formatter')
71-
);
52+
$instance = parent::create($container);
53+
$instance->{{ entity_class[:1]|lower ~ entity_class[1:] }}Storage = $container->get('entity_type.manager')->getStorage('{{ entity_name }}');
54+
$instance->dateFormatter = $container->get('date.formatter');
55+
return $instance;
7256
}
7357

7458
/**

templates/module/src/Entity/Form/entity-content.php.twig

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ namespace Drupal\{{module}}\Form;
99
{% endblock %}
1010

1111
{% block use_class %}
12-
use Drupal\Component\Datetime\TimeInterface;
1312
use Drupal\Core\Entity\ContentEntityForm;
14-
use Drupal\Core\Entity\EntityRepositoryInterface;
15-
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
1613
use Drupal\Core\Form\FormStateInterface;
17-
use Drupal\Core\Session\AccountProxyInterface;
1814
use Symfony\Component\DependencyInjection\ContainerInterface;
1915
{% endblock %}
2016

@@ -33,35 +29,14 @@ class {{ entity_class }}Form extends ContentEntityForm {% endblock %}
3329
*/
3430
protected $account;
3531

36-
/**
37-
* Constructs a new {{ entity_class }}Form.
38-
*
39-
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
40-
* The entity repository service.
41-
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
42-
* The entity type bundle service.
43-
* @param \Drupal\Component\Datetime\TimeInterface $time
44-
* The time service.
45-
* @param \Drupal\Core\Session\AccountProxyInterface $account
46-
* The current user account.
47-
*/
48-
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountProxyInterface $account) {
49-
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
50-
51-
$this->account = $account;
52-
}
53-
5432
/**
5533
* {@inheritdoc}
5634
*/
5735
public static function create(ContainerInterface $container) {
5836
// Instantiates this form class.
59-
return new static(
60-
$container->get('entity.repository'),
61-
$container->get('entity_type.bundle.info'),
62-
$container->get('datetime.time'),
63-
$container->get('current_user')
64-
);
37+
$instance = parent::create($container);
38+
$instance->account = $container->get('current_user');
39+
return $instance;
6540
}
6641

6742
/**

templates/module/src/Form/form-config.php.twig

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,28 @@ namespace Drupal\{{module_name}}\Form;
1212
use Drupal\Core\Form\ConfigFormBase;
1313
use Drupal\Core\Form\FormStateInterface;
1414
{% if services is not empty %}
15-
use Drupal\Core\Config\ConfigFactoryInterface;
1615
use Symfony\Component\DependencyInjection\ContainerInterface;
1716
{% endif %}
1817
{% endblock %}
1918

19+
{% block use_class_services %}
20+
{% endblock %}
21+
2022
{% block class_declaration %}
2123
/**
2224
* Class {{ class_name }}.
2325
*/
2426
class {{ class_name }} extends ConfigFormBase {% endblock %}
25-
{% block class_construct %}
26-
{% if services is not empty %}
27-
/**
28-
* Constructs a new {{ class_name }} object.
29-
*/
30-
public function __construct(
31-
ConfigFactoryInterface $config_factory,
32-
{{ servicesAsParameters(services)|join(',\n ') }}
33-
) {
34-
parent::__construct($config_factory);
35-
{{ serviceClassInitialization(services) }}
36-
}
37-
38-
{% endif %}
39-
{% endblock %}
4027

4128
{% block class_create %}
4229
{% if services is not empty %}
4330
/**
4431
* {@inheritdoc}
4532
*/
4633
public static function create(ContainerInterface $container) {
47-
return new static(
48-
$container->get('config.factory'),
49-
{{ serviceClassInjection(services) }}
50-
);
34+
$instance = parent::create($container);
35+
{{ serviceClassInjectionNoOverride(services) }}
36+
return $instance;
5137
}
5238

5339
{% endif %}

0 commit comments

Comments
 (0)