Skip to content

Commit 4e38c32

Browse files
authored
Merge pull request #143 from steveworley/patch-entity-view
Add support for entity_view
2 parents 0e3acf5 + a659782 commit 4e38c32

File tree

6 files changed

+177
-2
lines changed

6 files changed

+177
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ https://www.palantir.net/rector
2020

2121
## Scope and limitations
2222

23-
The development of this tool is prioritized by the percieved impact of the deprecations and updates. There are many deprecations that often involve several components and for each of these there are several ways to address the deprecation.
23+
The development of this tool is prioritized by the perceived impact of the deprecations and updates. There are many deprecations that often involve several components and for each of these there are several ways to address the deprecation.
2424

2525
We've tried to determine impact based on:
2626
- The use of the deprecated functionality in the contributed modules on Drupal.org
@@ -111,7 +111,7 @@ Fatal error: Declaration of _HumbugBox3630ef99eac4\Symfony\Component\HttpKernel\
111111
```
112112

113113
You may need to check that you are
114-
- Running `composer install` from an environment that support Php 7.2 or greater
114+
- Running `composer install` from an environment that supports Php 7.2 or greater
115115
- Running Drupal Rector from an environment that supports Php 7.2 or greater
116116

117117
Sometimes people install composer dependencies from one machine (host machine) and run Drupal Rector from another (such as a Lando VM).

config/drupal-8/drupal-8.0-deprecations.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use DrupalRector\Rector\Deprecation\EntityInterfaceUrlInfoRector;
1919
use DrupalRector\Rector\Deprecation\EntityLoadRector;
2020
use DrupalRector\Rector\Deprecation\EntityManagerRector;
21+
use DrupalRector\Rector\Deprecation\EntityViewRector;
2122
use DrupalRector\Rector\Deprecation\FileLoadRector;
2223
use DrupalRector\Rector\Deprecation\FormatDateRector;
2324
use DrupalRector\Rector\Deprecation\LinkGeneratorTraitLRector;
@@ -59,6 +60,8 @@
5960

6061
$services->set(EntityLoadRector::class);
6162

63+
$services->set(EntityViewRector::class);
64+
6265
$services->set(EntityManagerRector::class);
6366

6467
$services->set(FormatDateRector::class);

deprecation-index.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@
192192
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.'
193193
Examples:
194194
- entity_load.php
195+
'entity_view()':
196+
Rector: EntityViewRector.php
197+
PHPStan: 'Use the entity view builder''s view() method for creating a render array'
198+
Examples:
199+
- entity_view.php
195200
'node_load()':
196201
Rector: NodeLoadRector.php
197202
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().'

rector_examples/entity_view.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* This demonstrates the deprecated static calls that might be called from procedural code like `.module` files.
5+
*/
6+
7+
/**
8+
* A simple example.
9+
*/
10+
function simple_example() {
11+
$entity = new stdClass();
12+
$view = entity_view($entity, 'default');
13+
}
14+
15+
/**
16+
* An example using all of the arguments.
17+
*/
18+
function lagncode_example() {
19+
$entity = new stdClass();
20+
$langcode = 'de';
21+
$view = entity_view($entity, 'default', $langcode);
22+
}
23+
24+
function reset_cache_example() {
25+
// Not supported.
26+
}
27+
28+
/**
29+
* An example using arguments as member values.
30+
*/
31+
function arguments_member_values() {
32+
$entity = new stdClass();
33+
$entity->field_view_mode = 'test';
34+
35+
$view = entity_view($entity, $entity->field_view_mode);
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* This demonstrates the deprecated static calls that might be called from procedural code like `.module` files.
5+
*/
6+
7+
/**
8+
* A simple example.
9+
*/
10+
function simple_example() {
11+
$entity = new stdClass();
12+
$view = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, 'default');
13+
}
14+
15+
/**
16+
* An example using all of the arguments.
17+
*/
18+
function lagncode_example() {
19+
$entity = new stdClass();
20+
$langcode = 'de';
21+
$view = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, 'default', $langcode);
22+
}
23+
24+
function reset_cache_example() {
25+
// Not supported.
26+
}
27+
28+
/**
29+
* An example using arguments as member values.
30+
*/
31+
function arguments_member_values() {
32+
$entity = new stdClass();
33+
$entity->field_view_mode = 'test';
34+
35+
$view = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, $entity->field_view_mode);
36+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace DrupalRector\Rector\Deprecation;
4+
5+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
6+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
7+
use Rector\Core\Rector\AbstractRector;
8+
use PhpParser\Node;
9+
10+
11+
/**
12+
* Replaced deprecated entity_view() calls.
13+
*
14+
* See https://www.drupal.org/node/3033656 for change record.
15+
*
16+
* What is covered:
17+
* - Static replacement
18+
* - The reset parameter is excluded.
19+
*
20+
* Improvement opportunities
21+
* - Include support for cache rest parameter.
22+
*/
23+
final class EntityViewRector extends AbstractRector
24+
{
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
public function getNodeTypes(): array
30+
{
31+
return [
32+
Node\Expr\FuncCall::class,
33+
];
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function getRuleDefinition(): RuleDefinition
40+
{
41+
return new RuleDefinition('Fixes deprecated entity_view() use',[
42+
new CodeSample(
43+
<<<'CODE_BEFORE'
44+
$rendered = entity_view($entity, 'default');
45+
CODE_BEFORE
46+
,
47+
<<<'CODE_AFTER'
48+
$rendered = \Drupal::entityTypeManager()->getViewBuilder($entity
49+
->getEntityTypeId())->view($entity, 'default');
50+
CODE_AFTER
51+
)
52+
]);
53+
}
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
public function refactor(Node $node): ?Node
59+
{
60+
if ($this->getName($node->name) !== 'entity_view') {
61+
return NULL;
62+
}
63+
64+
$name = new Node\Name\FullyQualified('Drupal');
65+
66+
$entityTypManager = new Node\Identifier('entityTypeManager');
67+
68+
$var = new Node\Expr\StaticCall($name, $entityTypManager);
69+
70+
$getViewBuilder_method_name = new Node\Identifier('getViewBuilder');
71+
72+
$entity_reference = $node->args[0]->value;
73+
$getEntityTypeId_method_name = new Node\Identifier('getEntityTypeId');
74+
75+
$entityRef_type_id = new Node\Expr\MethodCall($entity_reference, $getEntityTypeId_method_name);
76+
77+
$view_builder = new Node\Expr\MethodCall($var, $getViewBuilder_method_name, [$entityRef_type_id]);
78+
79+
$view_method_name = new Node\Identifier('view');
80+
81+
$view_args = [
82+
$node->args[0]->value,
83+
$node->args[1]->value,
84+
];
85+
86+
if (isset($node->args[2])) {
87+
$view_args[] = $node->args[2]->value;
88+
}
89+
90+
$view = new Node\Expr\MethodCall($view_builder, $view_method_name, $view_args);
91+
92+
return $view;
93+
}
94+
95+
}

0 commit comments

Comments
 (0)