Skip to content
Snippets Groups Projects

Cleanup stuff reported by PHPStan

Files
7
@@ -6,15 +6,16 @@ namespace Drupal\form_decorator_example\FormDecorator;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\FormStateInterface;
use Drupal\form_decorator\FormDecoratorBase;
use Drupal\form_decorator\Attribute\FormDecorator;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\form_decorator\ContentEntityFormDecoratorBase;
use Drupal\node\NodeInterface;
/**
* Adds a created date picker to the node form.
*/
#[FormDecorator('form_node_form_alter')]
final class NodeCreatedDate extends FormDecoratorBase {
final class NodeCreatedDate extends ContentEntityFormDecoratorBase {
use StringTranslationTrait;
@@ -23,9 +24,11 @@ final class NodeCreatedDate extends FormDecoratorBase {
*/
public function buildForm(array $form, FormStateInterface $form_state, ...$args) {
$form = $this->inner->buildForm($form, $form_state, ...$args);
$node = $this->getEntity();
assert($node instanceof NodeInterface);
$created_time = time();
if (!$this->getEntity()->isNew()) {
$created_time = $this->getEntity()->getCreatedTime();
$created_time = $node->getCreatedTime();
}
$form['created_datepicker'] = [
@@ -40,8 +43,10 @@ final class NodeCreatedDate extends FormDecoratorBase {
/**
* {@inheritdoc}
*/
public function save(array &$form, FormStateInterface $form_state) {
$this->getEntity()->setCreatedTime($form_state->getValue('created_datepicker')->getTimestamp());
public function save(array $form, FormStateInterface $form_state) {
$node = $this->getEntity();
assert($node instanceof NodeInterface);
$node->setCreatedTime($form_state->getValue('created_datepicker')->getTimestamp());
return $this->inner->save($form, $form_state);
}
Loading