Conditions | 12 |
Paths | 19 |
Total Lines | 91 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
16 | function df_tools_panelizer_scenarios_migration_finished(Migration $migration) { |
||
17 | // Initialize variables required to parse migration. |
||
18 | /** @var Drupal\migrate_source_csv\Plugin\migrate\source\CSV $source */ |
||
19 | $source = $migration->getSourcePlugin(); |
||
20 | |||
21 | /** @var Drupal\migrate_source_csv\CSVFileObject $iterator */ |
||
22 | $iterator = $source->initializeIterator(); |
||
23 | $base_path = $iterator->getPath(); |
||
24 | $filename = $base_path . '/' . $iterator->getFilename(); |
||
25 | |||
26 | $destination = $migration->getDestinationPlugin(); |
||
27 | $entity_type = explode(':', $destination->getPluginId())[1]; |
||
28 | |||
29 | // We only support Nodes at this point. |
||
30 | // @todo Support more types if necessary. |
||
31 | if ($entity_type != 'node') { |
||
32 | return; |
||
33 | } |
||
34 | |||
35 | $entity_storage = \Drupal::entityManager()->getStorage($entity_type); |
||
|
|||
36 | |||
37 | // Determine the ID required to lookup entities in this migration. |
||
38 | $id_map = []; |
||
39 | $ids = array_keys($source->getIds()); |
||
40 | $processes = $migration->getProcess(); |
||
41 | |||
42 | foreach ($processes as $field => $process) { |
||
43 | if (isset($process[0]['source']) && $process[0]['source'] == $ids[0] && strpos($field, '/') === FALSE) { |
||
44 | $id_map = [$field, $ids[0]]; |
||
45 | } |
||
46 | } |
||
47 | |||
48 | // Grab the processed CSV file using existing CSV methods. |
||
49 | $file = new CSVFileObject($filename); |
||
50 | $file->setHeaderRowCount(1); |
||
51 | |||
52 | $count = $file->count(); |
||
53 | $file->rewind(); |
||
54 | $iterator->rewind(); |
||
55 | |||
56 | if (!empty($id_map)) { |
||
57 | for ($i = 0; $i < $count; ++$i) { |
||
58 | // Get the current row. |
||
59 | $source_row = $iterator->current(); |
||
60 | |||
61 | // Search for the Entity based on its identifier. |
||
62 | $field = $id_map[0]; |
||
63 | $value = $source_row[$id_map[1]]; |
||
64 | |||
65 | $ids = \Drupal::entityQuery($entity_type) |
||
66 | ->condition($field, $value) |
||
67 | ->execute(); |
||
68 | |||
69 | if (!empty($ids)) { |
||
70 | // Load the Entity, so we can grab field values and add the Panelizer display. |
||
71 | $id = reset($ids); |
||
72 | |||
73 | /** @var Drupal\Core\Entity\ContentEntityBase $entity */ |
||
74 | $entity = $entity_storage->load($id); |
||
75 | |||
76 | // See if a Panelizer config file exists for this Node. |
||
77 | $panels_file = $base_path . '/panelizer.panels_display.' . $entity_type . '.' . $entity->uuid() . '.yml'; |
||
78 | |||
79 | if (file_exists($panels_file) && isset($entity->panelizer)) { |
||
80 | $yaml = file_get_contents($panels_file); |
||
81 | |||
82 | $panels_display = Yaml::parse($yaml); |
||
83 | |||
84 | // The storage ID isn't based on UUIDs, unfortunately. Hotswap it with current info. |
||
85 | $panels_display['storage_id'] = $entity_type . ':' . $entity->id() . ':full:' . $entity->getRevisionId(); |
||
86 | |||
87 | // Set the Panels Display to match what's defined in YAML. |
||
88 | $entity->panelizer->panels_display = $panels_display; |
||
89 | |||
90 | // Mark that this is an overridden display. |
||
91 | $entity->panelizer->default = NULL; |
||
92 | |||
93 | // This is a required field in SQL with no default, but it always "full". |
||
94 | $entity->panelizer->view_mode = 'full'; |
||
95 | |||
96 | if ($entity instanceof RevisionLogInterface) { |
||
97 | $entity->setRevisionLogMessage(NULL); |
||
98 | } |
||
99 | |||
100 | $entity->save(); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | // Move to the next row. |
||
105 | $file->next(); |
||
106 | $iterator->next(); |
||
107 | } |
||
110 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.