diff --git a/modules/social_features/social_event/social_event.module b/modules/social_features/social_event/social_event.module index 6822d41b7ec..e2e8c4f3814 100644 --- a/modules/social_features/social_event/social_event.module +++ b/modules/social_features/social_event/social_event.module @@ -29,7 +29,6 @@ use Drupal\menu_link_content\MenuLinkContentInterface; use Drupal\node\Entity\Node; use Drupal\node\NodeInterface; use Drupal\social_event\Controller\SocialEventController; -use Drupal\social_event\EdaHandler; use Drupal\social_event\Entity\Node\Event; use Drupal\social_event\EventEnrollmentInterface; use Drupal\social_event\SocialEventDateAllDay; @@ -504,9 +503,9 @@ function social_event_views_data_alter(array &$data) { /** * Implements hook_ENTITY_TYPE_insert(). */ -function social_event_node_insert(EntityInterface $entity): void { - if ($entity->bundle() == 'event') { - \Drupal::service('social_event.eda_handler')->eventCreate($entity); +function social_event_node_presave(NodeInterface $node): void { + if ($node->bundle() == 'event') { + \Drupal::service('social_event.eda_handler')->eventCreate($node); } } diff --git a/modules/social_features/social_event/src/EdaHandler.php b/modules/social_features/social_event/src/EdaHandler.php index 4dd0750756f..06891cfa63e 100644 --- a/modules/social_features/social_event/src/EdaHandler.php +++ b/modules/social_features/social_event/src/EdaHandler.php @@ -4,8 +4,8 @@ use CloudEvents\V1\CloudEvent; use Drupal\Component\Uuid\UuidInterface; -use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\node\NodeInterface; use Drupal\social_eda\Types\Address; use Drupal\social_eda\Types\DateTime; use Drupal\social_eda\Types\Entity; @@ -34,18 +34,18 @@ final class EdaHandler { * {@inheritDoc} */ public function __construct( - private ?SocialEdaDispatcher $dispatcher, - private UuidInterface $uuid, - private RequestStack $requestStack, - private ModuleHandlerInterface $moduleHandler, + private readonly ?SocialEdaDispatcher $dispatcher, + private readonly UuidInterface $uuid, + private readonly RequestStack $requestStack, + private readonly ModuleHandlerInterface $moduleHandler, ) {} /** * Create event handler. */ - public function eventCreate(EntityInterface $entity): void { + public function eventCreate(NodeInterface $node): void { // Skip if required modules are not enabled. - if (!$this->moduleHandler->moduleExists('social_eda') && !$this->moduleHandler->moduleExists('social_eda_dispatcher')) { + if (!$this->moduleHandler->moduleExists('social_eda') || !$this->dispatcher) { return; } @@ -64,38 +64,38 @@ public function eventCreate(EntityInterface $entity): void { type: self::EVENT_TYPE, data: [ 'event' => new EventCreateEventData( - id: $entity->uuid->value, - created: DateTime::fromTimestamp($entity->created->value)->toString(), - updated: DateTime::fromTimestamp($entity->changed->value)->toString(), - status: $entity->status->value, - label: (string) $entity->label(), - visibility: $entity->field_content_visibility->value, - group: !$entity->groups->isEmpty() ? Entity::fromEntity($entity->groups->entity) : NULL, - author: User::fromEntity($entity->uid->entity), - allDay: $entity->field_event_all_day->value, - start: $entity->field_event_date->value, - end: $entity->field_event_date_end->value, + id: $node->get('uuid')->value, + created: DateTime::fromTimestamp($node->getCreatedTime())->toString(), + updated: DateTime::fromTimestamp($node->getChangedTime())->toString(), + status: $node->get('status')->value, + label: (string) $node->label(), + visibility: $node->get('field_content_visibility')->value, + group: !$node->get('groups')->isEmpty() ? Entity::fromEntity($node->get('groups')->entity) : NULL, + author: User::fromEntity($node->get('uid')->entity), + allDay: $node->get('field_event_all_day')->value, + start: $node->get('field_event_date')->value, + end: $node->get('field_event_date_end')->value, timezone: date_default_timezone_get(), address: Address::fromFieldItem( - item: $entity->field_event_address->first(), - label: $entity->field_event_location->value + item: $node->get('field_event_address')->first(), + label: $node->get('field_event_location')->value ), enrollment: [ - 'enabled' => (bool) $entity->field_event_enroll->value, - 'method' => $enrollment_methods[$entity->field_enroll_method->value], + 'enabled' => (bool) $node->get('field_event_enroll')->value, + 'method' => $enrollment_methods[$node->get('field_enroll_method')->value], ], - href: Href::fromEntity($entity), - type: $entity->hasField('field_event_type') && !$entity->field_event_type->isEmpty() ? $entity->field_event_type->entity->label() : NULL, + href: Href::fromEntity($node), + type: $node->hasField('field_event_type') && !$node->get('field_event_type')->isEmpty() ? $node->get('field_event_type')->entity->label() : NULL, ), 'actor' => [ 'application' => NULL, - 'user' => User::fromEntity($entity->uid->entity), + 'user' => User::fromEntity($node->get('uid')->entity), ], ], dataContentType: 'application/json', dataSchema: NULL, subject: NULL, - time: DateTime::fromTimestamp($entity->created->value)->toImmutableDateTime(), + time: DateTime::fromTimestamp($node->getCreatedTime())->toImmutableDateTime(), ) ); } diff --git a/phpstan.neon b/phpstan.neon index 7d139fadd79..c4a6c169642 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -68,3 +68,14 @@ parameters: ignoreErrors: # See - https://github.com/mglaman/drupal-check/pull/187 - '#Unsafe usage of new static\(\)#' + + # Stub files provide type information for classes and functions that are + # otherwise difficult to analyze, improving the accuracy of PHPStan's checks. + stubFiles: + - src/PHPStan/Stubs/social_eda/Address.stub + - src/PHPStan/Stubs/social_eda/DateTime.stub + - src/PHPStan/Stubs/social_eda/Entity.stub + - src/PHPStan/Stubs/social_eda/Href.stub + - src/PHPStan/Stubs/social_eda/User.stub + - src/PHPStan/Stubs/social_eda_dispatcher/Dispatcher.stub + - src/PHPStan/Stubs/social_eda_dispatcher/CloudEvent.stub diff --git a/src/PHPStan/Stubs/social_eda/Address.stub b/src/PHPStan/Stubs/social_eda/Address.stub new file mode 100644 index 00000000000..63fc787cfb0 --- /dev/null +++ b/src/PHPStan/Stubs/social_eda/Address.stub @@ -0,0 +1,19 @@ +