Complex classes like FieldValueManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FieldValueManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class FieldValueManager implements FieldValueManagerInterface { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The storage plugin for the paragraph entity type. |
||
| 21 | * |
||
| 22 | * @var \Drupal\Core\Entity\EntityStorageInterface |
||
| 23 | */ |
||
| 24 | protected $storage; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The storage plugin for the paragraph type config entity type. |
||
| 28 | * |
||
| 29 | * @var \Drupal\Core\Entity\EntityStorageInterface |
||
| 30 | */ |
||
| 31 | protected $bundleStorage; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The field value manager service for collecting field information. |
||
| 35 | * |
||
| 36 | * @var \Drupal\Core\Entity\EntityFieldManagerInterface |
||
| 37 | */ |
||
| 38 | protected $entityFieldManager; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Element definitions for custom elements that can occur in an editor field. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $elements; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * A static cache of paragraph revisions. |
||
| 49 | * |
||
| 50 | * @var \Drupal\paragraphs\ParagraphInterface[] |
||
| 51 | */ |
||
| 52 | protected $revisionCache = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Creates a field value manager object. |
||
| 56 | * |
||
| 57 | * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager |
||
| 58 | * The field manager service. |
||
| 59 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
||
| 60 | * The entity type manager service. |
||
| 61 | * @param array $elements |
||
| 62 | * An array of widget binder element definitions. |
||
| 63 | */ |
||
| 64 | public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, array $elements) { |
||
| 65 | $this->entityFieldManager = $entity_field_manager; |
||
| 66 | $this->storage = $entity_type_manager->getStorage('paragraph'); |
||
| 67 | $this->bundleStorage = $entity_type_manager->getStorage('paragraphs_type'); |
||
| 68 | $this->elements = $elements; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | public function getReferencedEntities(EntityReferenceRevisionsFieldItemList $items) { |
||
| 75 | $entities = []; |
||
| 76 | foreach ($items as $item) { |
||
| 77 | $value = $item->getValue(); |
||
| 78 | if (!empty($value['entity']) && $value['entity'] instanceof ParagraphInterface) { |
||
| 79 | $entities[] = $item->entity; |
||
| 80 | } |
||
| 81 | elseif ($item->target_revision_id !== NULL) { |
||
| 82 | if (!empty($this->revisionCache[$item->target_revision_id])) { |
||
| 83 | $entities[] = $this->revisionCache[$item->target_revision_id]; |
||
| 84 | } |
||
| 85 | else { |
||
| 86 | $entity = $this->storage->loadRevision($item->target_revision_id); |
||
| 87 | $entity = $this->ensureParagraph($entity); |
||
| 88 | $this->revisionCache[$item->target_revision_id] = $entity; |
||
| 89 | $entities[] = $entity; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | return $entities; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | public function wrapItems(EntityReferenceRevisionsFieldItemList $items) { |
||
| 100 | $field_definition = $items->getFieldDefinition(); |
||
| 101 | if (!$this->isParagraphsEditorField($field_definition)) { |
||
| 102 | throw new \Exception('Attempt to wrap non-paragraphs editor field.'); |
||
| 103 | } |
||
| 104 | |||
| 105 | // Build a list of refrenced entities and filter out the text entities. |
||
| 106 | $settings = $field_definition->getThirdPartySettings('paragraphs_editor'); |
||
| 107 | $markup = ''; |
||
| 108 | $entities = []; |
||
| 109 | $text_entity = NULL; |
||
| 110 | |||
| 111 | foreach ($this->getReferencedEntities($items) as $entity) { |
||
| 112 | if ($entity->bundle() == $settings['text_bundle']) { |
||
| 113 | $markup .= $entity->{$settings['text_field']}->value; |
||
| 114 | if (!$text_entity) { |
||
| 115 | $text_entity = $entity; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | else { |
||
| 119 | $entities[$entity->uuid()] = $entity; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | // If there is no text entity we need to create one. |
||
| 124 | if (!$text_entity) { |
||
| 125 | $text_entity = $this->ensureParagraph($this->storage->create([ |
||
| 126 | 'type' => $settings['text_bundle'], |
||
| 127 | ])); |
||
| 128 | } |
||
| 129 | |||
| 130 | // Reset the text entity markup in case we merged multiple text entities. |
||
| 131 | $text_entity->{$settings['text_field']}->value = $markup; |
||
| 132 | if (empty($text_entity->{$settings['text_field']}->format) && !empty($settings['filter_format'])) { |
||
| 133 | $text_entity->{$settings['text_field']}->format = $settings['filter_format']; |
||
| 134 | } |
||
| 135 | |||
| 136 | return new FieldValueWrapper($field_definition, $text_entity, $entities); |
||
|
|
|||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | public function prepareEntityForSave($entity, $new_revision, $langcode) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | public function setItems(EntityReferenceRevisionsFieldItemList $items, array $entities, $new_revision = FALSE, $langcode = NULL) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | public function getTextBundles(array $allowed_bundles = []) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritdoc} |
||
| 209 | */ |
||
| 210 | public function isParagraphsField(FieldDefinitionInterface $field_definition) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | */ |
||
| 224 | public function isParagraphsEditorField(FieldDefinitionInterface $field_definition) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | public function getTextFields($bundle_name) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | public function getElement($element_name) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function getAttributeName($element_name, $attribute_name) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | public function getSelector($element_name) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Helper function to check if a field is a text field. |
||
| 295 | * |
||
| 296 | * @param \Drupal\Core\Field\FieldDefinitionInterface $field_config |
||
| 297 | * The field to check. |
||
| 298 | * |
||
| 299 | * @return bool |
||
| 300 | * TRUE if it's a paragraphs editor approved text field, FALSE otherwise. |
||
| 301 | */ |
||
| 302 | protected function isTextField(FieldDefinitionInterface $field_config) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Enforces that an entity is a paragraph entity. |
||
| 308 | * |
||
| 309 | * @return \Drupal\paragraphs\ParagraphInterface|null |
||
| 310 | * The filtered entity. |
||
| 311 | */ |
||
| 312 | protected function ensureParagraph(EntityInterface $entity = NULL) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Enforces that an entity is a field config entity. |
||
| 321 | * |
||
| 322 | * @param \Drupal\Core\Field\FieldDefinitionInterface|null $field_definition |
||
| 323 | * |
||
| 324 | * @return \Drupal\Core\Field\FieldConfigInterface |
||
| 325 | * The config object. |
||
| 326 | */ |
||
| 327 | protected function ensureFieldConfig(FieldDefinitionInterface $field_definition = NULL) { |
||
| 333 | |||
| 334 | } |
||
| 335 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.