3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\drupalmoduleupgrader\ConverterBase;
7 use Drupal\drupalmoduleupgrader\TargetInterface;
8 use Drupal\drupalmoduleupgrader\Utility\StringTransformTrait;
12 * id = "hook_entity_info",
13 * description = @Translation("Creates entity class boilerplate from hook_entity_info()."),
14 * hook = "hook_entity_info"
17 class HookEntityInfo extends ConverterBase {
19 use StringTransformTrait;
24 public function convert(TargetInterface $target) {
26 $entity_types = $this->executeHook($target, 'entity_info');
28 catch (\LogicException $e) {
29 $this->log->warning($e->getMessage(), [
30 'target' => $target->id(),
31 'hook' => $this->pluginDefinition['hook'],
36 foreach ($entity_types as $id => $entity_type) {
37 $entity_type['id'] = $id;
39 $entity_type['base_table'] = $entity_type['base table'];
40 unset($entity_type['base table']);
42 $entity_type['keys'] = $entity_type['entity keys'];
43 unset($entity_type['entity keys']);
45 if (isset($entity_type['controller class'])) {
46 /** @var \Pharborist\Objects\ClassNode $controller */
47 $indexer = $target->getIndexer('class');
48 if ($indexer->has($entity_type['controller class'])) {
49 $controller = $indexer->get($entity_type['controller class']);
51 $parent = $controller->getExtends();
53 if ($parent->getText() == 'DrupalDefaultEntityController' || $parent->getText() == 'EntityAPIController') {
54 $controller->setExtends('Drupal\Core\Entity\Sql\SqlContentEntityStorage');
57 // @todo Not entirely sure what to do here. It's not a huge problem
58 // if the controller extends another class defined by the target
59 // (which is, admittedly, an edge case), but if it extends a
60 // controller defined by *another* module that isn't Entity API?
64 // @todo Handle interfaces implemented by the entity controller.
66 $this->writeClass($target, PSR4::toPSR4($target, $controller));
67 $entity_type['controllers']['storage'] = $controller->getName()->getAbsolutePath();
70 throw new \LogicException(SafeMarkup::format('Cannot get ahold of the controller class for @entity_type entity type.', ['@entity_type' => $id]));
74 $entity_type['controllers']['storage'] = 'Drupal\Core\Entity\Sql\SqlContentEntityStorage';
78 '#module' => $target->id(),
79 '#class' => $this->toTitleCase($id),
80 '#theme' => 'dmu_entity_type',
81 '#info' => $entity_type,
83 $this->writeClass($target, $this->parse($render));