Skip to content

Commit

Permalink
Removes constructor override on service object generators. (#4174)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
GueGuerreiro authored and enzolutions committed Oct 11, 2019
1 parent ee6ad4a commit 0fe1993
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 411 deletions.
21 changes: 7 additions & 14 deletions templates/module/src/Controller/controller.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,25 @@ use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endblock %}

{% block use_class_services %}
{% endblock %}

{% block class_declaration %}
/**
* Class {{ class_name }}.
*/
class {{ class_name }} extends ControllerBase {% endblock %}
{% block class_construct %}
{% if services is not empty %}

/**
* Constructs a new {{ class_name }} object.
*/
public function __construct({{ servicesAsParameters(services)|join(', ') }}) {
{{ serviceClassInitialization(services) }}
}
{% endif %}
{% endblock %}
{% block class_create %}
{% if services is not empty %}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
{{ serviceClassInjection(services) }}
);
$instance = parent::create($container);
{{ serviceClassInjectionNoOverride(services) }}
return $instance;
}

{% endif %}
Expand Down
24 changes: 4 additions & 20 deletions templates/module/src/Controller/entity-controller.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ namespace Drupal\{{ module }}\Controller;
{% block use_class %}
use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Url;
use Drupal\{{ module }}\Entity\{{ entity_class }}Interface;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand All @@ -26,7 +24,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class {{ entity_class }}Controller extends ControllerBase implements ContainerInjectionInterface {% endblock %}
{% block class_methods %}

/**
* The date formatter.
*
Expand All @@ -41,27 +38,14 @@ class {{ entity_class }}Controller extends ControllerBase implements ContainerIn
*/
protected $renderer;

/**
* Constructs a new {{ entity_class }}Controller.
*
* @param \Drupal\Core\Datetime\DateFormatter $date_formatter
* The date formatter.
* @param \Drupal\Core\Render\Renderer $renderer
* The renderer.
*/
public function __construct(DateFormatter $date_formatter, Renderer $renderer) {
$this->dateFormatter = $date_formatter;
$this->renderer = $renderer;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('date.formatter'),
$container->get('renderer')
);
$instance = parent::create($container);
$instance->dateFormatter = $container->get('date.formatter');
$instance->renderer = $container->get('renderer');
return $instance;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace Drupal\{{module}}\Form;
{% endblock %}

{% block use_class %}
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
Expand All @@ -25,7 +23,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class {{ entity_class }}RevisionDeleteForm extends ConfirmFormBase {% endblock %}
{% block class_methods %}

/**
* The {{ label }} revision.
*
Expand All @@ -38,7 +35,7 @@ class {{ entity_class }}RevisionDeleteForm extends ConfirmFormBase {% endblock %
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected ${{ entity_class }}Storage;
protected ${{ entity_class[:1]|lower ~ entity_class[1:] }}Storage;

/**
* The database connection.
Expand All @@ -47,28 +44,14 @@ class {{ entity_class }}RevisionDeleteForm extends ConfirmFormBase {% endblock %
*/
protected $connection;

/**
* Constructs a new {{ entity_class }}RevisionDeleteForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The entity storage.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
*/
public function __construct(EntityStorageInterface $entity_storage, Connection $connection) {
$this->{{ entity_class }}Storage = $entity_storage;
$this->connection = $connection;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$entity_type_manager = $container->get('entity_type.manager');
return new static(
$entity_type_manager->getStorage('{{ entity_name }}'),
$container->get('database')
);
$instance = parent::create($container);
$instance->{{ entity_class[:1]|lower ~ entity_class[1:] }}Storage = $container->get('entity_type.manager')->getStorage('{{ entity_name }}');
$instance->connection = $container->get('database');
return $instance;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ namespace Drupal\{{module}}\Form;
{% endblock %}

{% block use_class %}
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\{{module}}\Entity\{{ entity_class }}Interface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endblock %}
Expand All @@ -25,7 +22,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class {{ entity_class }}RevisionRevertTranslationForm extends {{ entity_class }}RevisionRevertForm {% endblock %}
{% block class_methods %}

/**
* The language to be reverted.
*
Expand All @@ -40,30 +36,13 @@ class {{ entity_class }}RevisionRevertTranslationForm extends {{ entity_class }}
*/
protected $languageManager;

/**
* Constructs a new {{ entity_class }}RevisionRevertTranslationForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The {{ label }} storage.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter, LanguageManagerInterface $language_manager) {
parent::__construct($entity_storage, $date_formatter);
$this->languageManager = $language_manager;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')->getStorage('{{ entity_name }}'),
$container->get('date.formatter'),
$container->get('language_manager')
);
$instance = parent::create($container);
$instance->languageManager = $container->get('language_manager');
return $instance;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace Drupal\{{module}}\Form;
{% endblock %}

{% block use_class %}
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
Expand All @@ -26,7 +24,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %}
{% block class_methods %}

/**
* The {{ label }} revision.
*
Expand All @@ -39,7 +36,7 @@ class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected ${{ entity_class }}Storage;
protected ${{ entity_class[:1]|lower ~ entity_class[1:] }}Storage;

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

/**
* Constructs a new {{ entity_class }}RevisionRevertForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The {{ label }} storage.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter) {
$this->{{ entity_class }}Storage = $entity_storage;
$this->dateFormatter = $date_formatter;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')->getStorage('{{ entity_name }}'),
$container->get('date.formatter')
);
$instance = parent::create($container);
$instance->{{ entity_class[:1]|lower ~ entity_class[1:] }}Storage = $container->get('entity_type.manager')->getStorage('{{ entity_name }}');
$instance->dateFormatter = $container->get('date.formatter');
return $instance;
}

/**
Expand Down
31 changes: 3 additions & 28 deletions templates/module/src/Entity/Form/entity-content.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ namespace Drupal\{{module}}\Form;
{% endblock %}

{% block use_class %}
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endblock %}

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

/**
* Constructs a new {{ entity_class }}Form.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Drupal\Core\Session\AccountProxyInterface $account
* The current user account.
*/
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountProxyInterface $account) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);

$this->account = $account;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this form class.
return new static(
$container->get('entity.repository'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
$container->get('current_user')
);
$instance = parent::create($container);
$instance->account = $container->get('current_user');
return $instance;
}

/**
Expand Down
26 changes: 6 additions & 20 deletions templates/module/src/Form/form-config.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,28 @@ namespace Drupal\{{module_name}}\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
{% if services is not empty %}
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endblock %}

{% block use_class_services %}
{% endblock %}

{% block class_declaration %}
/**
* Class {{ class_name }}.
*/
class {{ class_name }} extends ConfigFormBase {% endblock %}
{% block class_construct %}
{% if services is not empty %}
/**
* Constructs a new {{ class_name }} object.
*/
public function __construct(
ConfigFactoryInterface $config_factory,
{{ servicesAsParameters(services)|join(',\n ') }}
) {
parent::__construct($config_factory);
{{ serviceClassInitialization(services) }}
}

{% endif %}
{% endblock %}

{% block class_create %}
{% if services is not empty %}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
{{ serviceClassInjection(services) }}
);
$instance = parent::create($container);
{{ serviceClassInjectionNoOverride(services) }}
return $instance;
}

{% endif %}
Expand Down
Loading

0 comments on commit 0fe1993

Please sign in to comment.