diff --git a/config/drupal-8/drupal-8.0-deprecations.php b/config/drupal-8/drupal-8.0-deprecations.php index a1dcf0de..f02d31b9 100644 --- a/config/drupal-8/drupal-8.0-deprecations.php +++ b/config/drupal-8/drupal-8.0-deprecations.php @@ -18,6 +18,7 @@ use DrupalRector\Rector\Deprecation\EntityInterfaceUrlInfoRector; use DrupalRector\Rector\Deprecation\EntityLoadRector; use DrupalRector\Rector\Deprecation\EntityManagerRector; +use DrupalRector\Rector\Deprecation\EntityViewRector; use DrupalRector\Rector\Deprecation\FileLoadRector; use DrupalRector\Rector\Deprecation\FormatDateRector; use DrupalRector\Rector\Deprecation\LinkGeneratorTraitLRector; @@ -59,6 +60,8 @@ $services->set(EntityLoadRector::class); + $services->set(EntityViewRector::class); + $services->set(EntityManagerRector::class); $services->set(FormatDateRector::class); diff --git a/deprecation-index.yml b/deprecation-index.yml index aa01ec2c..da3c1195 100644 --- a/deprecation-index.yml +++ b/deprecation-index.yml @@ -192,6 +192,11 @@ PHPStan: 'Call to deprecated function entity_load(). Deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the entity type storage''s load() method.' Examples: - entity_load.php +'entity_view()': + Rector: EntityViewRector.php + PHPStan: 'Use the entity view builder''s view() method for creating a render array' + Examples: + - entity_view.php 'node_load()': Rector: NodeLoadRector.php PHPStan: 'Call to deprecated function node_load(). Deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use Drupal\node\Entity\Node::load().' diff --git a/rector_examples/entity_view.php b/rector_examples/entity_view.php new file mode 100644 index 00000000..b128a6f3 --- /dev/null +++ b/rector_examples/entity_view.php @@ -0,0 +1,36 @@ +field_view_mode = 'test'; + + $view = entity_view($entity, $entity->field_view_mode); +} diff --git a/rector_examples_updated/entity_view.php b/rector_examples_updated/entity_view.php new file mode 100644 index 00000000..7358e48a --- /dev/null +++ b/rector_examples_updated/entity_view.php @@ -0,0 +1,36 @@ +getViewBuilder($entity->getEntityTypeId())->view($entity, 'default'); +} + +/** + * An example using all of the arguments. + */ +function lagncode_example() { + $entity = new stdClass(); + $langcode = 'de'; + $view = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, 'default', $langcode); +} + +function reset_cache_example() { + // Not supported. +} + +/** + * An example using arguments as member values. + */ +function arguments_member_values() { + $entity = new stdClass(); + $entity->field_view_mode = 'test'; + + $view = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, $entity->field_view_mode); +} diff --git a/src/Rector/Deprecation/EntityViewRector.php b/src/Rector/Deprecation/EntityViewRector.php new file mode 100644 index 00000000..4cab2d35 --- /dev/null +++ b/src/Rector/Deprecation/EntityViewRector.php @@ -0,0 +1,95 @@ +getViewBuilder($entity + ->getEntityTypeId())->view($entity, 'default'); +CODE_AFTER + ) + ]); + } + + /** + * @inheritdoc + */ + public function refactor(Node $node): ?Node + { + if ($this->getName($node->name) !== 'entity_view') { + return NULL; + } + + $name = new Node\Name\FullyQualified('Drupal'); + + $entityTypManager = new Node\Identifier('entityTypeManager'); + + $var = new Node\Expr\StaticCall($name, $entityTypManager); + + $getViewBuilder_method_name = new Node\Identifier('getViewBuilder'); + + $entity_reference = $node->args[0]->value; + $getEntityTypeId_method_name = new Node\Identifier('getEntityTypeId'); + + $entityRef_type_id = new Node\Expr\MethodCall($entity_reference, $getEntityTypeId_method_name); + + $view_builder = new Node\Expr\MethodCall($var, $getViewBuilder_method_name, [$entityRef_type_id]); + + $view_method_name = new Node\Identifier('view'); + + $view_args = [ + $node->args[0]->value, + $node->args[1]->value, + ]; + + if (isset($node->args[2])) { + $view_args[] = $node->args[2]->value; + } + + $view = new Node\Expr\MethodCall($view_builder, $view_method_name, $view_args); + + return $view; + } + +}