Skip to content

Commit

Permalink
Code Cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
attus74 committed Jun 23, 2023
1 parent e7ddee0 commit a1b0571
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ drush devu-nt-bundle entity_type_id bundle_id "Bundle Label" --name="Your Name"
## New Annotation Plugin
```
drush devu-plugin plugin_name --module=existing_module_name --name="Your Name"
```
```
You're free to use this module without any restriction but without any warranty.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"license": "GPL-3.0-or-later",
"require": {
"drupal/core": "^10.0 || ^11.0",
"drupal/core": "^10.1 || ^11.0",
"nikic/php-parser": "^4.0"
},
"extra": {
Expand Down
36 changes: 33 additions & 3 deletions src/ConfigEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ protected function _createForms(): void
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$factory = new BuilderFactory();

///// Entity Form

$createCode = <<<'CODE'
<?php
Expand Down Expand Up @@ -246,7 +248,7 @@ protected function _createForms(): void
$formStatement = $factory->method('form')
->makePublic()
->setDocComment('/** @{inheritdoc} */')
->addParam($factory->param('form')->setType('array')->makeByRef())
->addParam($factory->param('form')->setType('array'))
->addParam($factory->param('form_state')->setType('FormStateInterface'));
foreach($formAst as $command) {
$formStatement->addStmt($command);
Expand All @@ -266,7 +268,8 @@ protected function _createForms(): void
]));
}
$form_state->setRedirect("entity.' . $this->_entityTypeName . '.collection");';
$form_state->setRedirect("entity.' . $this->_entityTypeName . '.collection");
return $status;';
$saveCode = <<<CODE
<?php
Expand All @@ -284,6 +287,7 @@ protected function _createForms(): void
}

$existCode = '$entity = $this->entityTypeManager->getStorage(\'' . $this->_entityTypeName . '\')->getQuery()
->accessCheck(false)
->condition("id", $id)
->execute();
return (bool) $entity;';
Expand Down Expand Up @@ -328,6 +332,17 @@ protected function _createForms(): void
->getNode();
$this->_savePhp($this->_getEntityNameClass() . 'Form', $formNode, 'Form');

///// Entity Delete Form

$deleteCreateCode = <<<'CODE'
<?php
return new static(
$container->get('messenger'),
);
CODE;
$deleteCreateAst = $parser->parse($deleteCreateCode);

$getQuestionCode = <<<'CODE'
<?php
Expand Down Expand Up @@ -356,7 +371,7 @@ protected function _createForms(): void
$submitFormCode = <<<'CODE'
<?php
$this->entity->delete();
\Drupal::messenger()->addMessage($this->t('%label has been deleted.', array('%label' => $this->entity->label())));
$this->_messenger->addMessage($this->t('%label has been deleted.', array('%label' => $this->entity->label())));
$form_state->setRedirectUrl($this->getCancelUrl());
CODE;
$submitFormAst = $parser->parse($submitFormCode);
Expand All @@ -371,13 +386,28 @@ protected function _createForms(): void

$deleteFormClass = $factory->class($this->_getEntityNameClass() . 'DeleteForm')
->extend('EntityConfirmFormBase')
->addStmt($factory->property('_messenger')
->makePrivate())
->addStmt($factory->method('__construct')
->makePublic()
->addParam($factory->param('messenger')
->setType('MessengerInterface '))
->addStmt(new Assign(new Variable('this->_messenger'), new Variable('messenger'))))
->addStmt($factory->method('create')
->makePublic()
->makeStatic()
->addParam($factory->param('container')
->setType('ContainerInterface'))
->addStmt(current($deleteCreateAst)))
->addStmt($getQuestionStatement)
->addStmt($cancelUrlStatement)
->addStmt($submitFormStatement);
$deleteFormNode = $factory->namespace('Drupal\\' . $this->_moduleName . '\\Form')
->addStmt($factory->use('Symfony\Component\DependencyInjection\ContainerInterface'))
->addStmt($factory->use('Drupal\Core\Entity\EntityConfirmFormBase'))
->addStmt($factory->use('Drupal\Core\Url'))
->addStmt($factory->use('Drupal\Core\Form\FormStateInterface'))
->addStmt($factory->use('Drupal\Core\Messenger\MessengerInterface'))
->setDocComment($this->_getDocComment($this->_entityTypeLabel . ' Delete Form'))
->addStmt($deleteFormClass)
->getNode();
Expand Down

0 comments on commit a1b0571

Please sign in to comment.