Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EntityReference 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 EntityReference, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class EntityReference extends WidgetBase implements ContainerFactoryPluginInterface { |
||
41 | |||
42 | /** |
||
43 | * Entity manager service |
||
44 | * |
||
45 | * @var \Drupal\Core\Entity\EntityManagerInterface |
||
46 | */ |
||
47 | protected $entityManager; |
||
48 | |||
49 | /** |
||
50 | * Field widget display plugin manager. |
||
51 | * |
||
52 | * @var \Drupal\entity_browser\FieldWidgetDisplayManager |
||
53 | */ |
||
54 | protected $fieldDisplayManager; |
||
55 | |||
56 | /** |
||
57 | * The depth of the delete button. |
||
58 | * |
||
59 | * This property exists so it can be changed if subclasses |
||
60 | * |
||
61 | * @var int |
||
62 | */ |
||
63 | protected static $deleteDepth = 4; |
||
64 | |||
65 | /** |
||
66 | * Constructs widget plugin. |
||
67 | * |
||
68 | * @param string $plugin_id |
||
69 | * The plugin_id for the plugin instance. |
||
70 | * @param mixed $plugin_definition |
||
71 | * The plugin implementation definition. |
||
72 | * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition |
||
73 | * The definition of the field to which the widget is associated. |
||
74 | * @param array $settings |
||
75 | * The widget settings. |
||
76 | * @param array $third_party_settings |
||
77 | * Any third party settings. |
||
78 | * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager |
||
79 | * Entity manager service. |
||
80 | * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
||
81 | * Event dispatcher. |
||
82 | * @param \Drupal\entity_browser\FieldWidgetDisplayManager $field_display_manager |
||
83 | * Field widget display plugin manager. |
||
84 | */ |
||
85 | public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityManagerInterface $entity_manager, EventDispatcherInterface $event_dispatcher, FieldWidgetDisplayManager $field_display_manager) { |
||
86 | parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings); |
||
87 | $this->entityManager = $entity_manager; |
||
88 | $this->fieldDisplayManager = $field_display_manager; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||
95 | return new static( |
||
96 | $plugin_id, |
||
97 | $plugin_definition, |
||
98 | $configuration['field_definition'], |
||
99 | $configuration['settings'], |
||
100 | $configuration['third_party_settings'], |
||
101 | $container->get('entity.manager'), |
||
102 | $container->get('event_dispatcher'), |
||
103 | $container->get('plugin.manager.entity_browser.field_widget_display') |
||
104 | ); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public static function defaultSettings() { |
||
111 | return array( |
||
112 | 'entity_browser' => NULL, |
||
113 | 'open' => FALSE, |
||
114 | 'field_widget_display' => NULL, |
||
115 | 'field_widget_edit' => TRUE, |
||
116 | 'field_widget_remove' => TRUE, |
||
117 | 'field_widget_display_settings' => [], |
||
118 | ) + parent::defaultSettings(); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function settingsForm(array $form, FormStateInterface $form_state) { |
||
125 | $element = parent::settingsForm($form, $form_state); |
||
126 | |||
127 | $browsers = []; |
||
128 | /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */ |
||
129 | foreach ($this->entityManager->getStorage('entity_browser')->loadMultiple() as $browser) { |
||
130 | $browsers[$browser->id()] = $browser->label(); |
||
131 | } |
||
132 | |||
133 | $element['entity_browser'] = [ |
||
134 | '#title' => t('Entity browser'), |
||
135 | '#type' => 'select', |
||
136 | '#default_value' => $this->getSetting('entity_browser'), |
||
137 | '#options' => $browsers, |
||
138 | ]; |
||
139 | |||
140 | $target_type = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type'); |
||
141 | $entity_type = \Drupal::entityTypeManager()->getStorage($target_type)->getEntityType(); |
||
142 | |||
143 | $displays = []; |
||
144 | foreach ($this->fieldDisplayManager->getDefinitions() as $id => $definition) { |
||
145 | if ($this->fieldDisplayManager->createInstance($id)->isApplicable($entity_type)) { |
||
146 | $displays[$id] = $definition['label']; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | $id = Html::getUniqueId('field-' . $this->fieldDefinition->getName() . '-display-settings-wrapper'); |
||
151 | $element['field_widget_display'] = [ |
||
152 | '#title' => t('Entity display plugin'), |
||
153 | '#type' => 'select', |
||
154 | '#default_value' => $this->getSetting('field_widget_display'), |
||
155 | '#options' => $displays, |
||
156 | '#ajax' => [ |
||
157 | 'callback' => array($this, 'updateSettingsAjax'), |
||
158 | 'wrapper' => $id, |
||
159 | ], |
||
160 | ]; |
||
161 | |||
162 | $element['field_widget_edit'] = [ |
||
163 | '#title' => t('Display Edit button'), |
||
164 | '#type' => 'checkbox', |
||
165 | '#default_value' => $this->getSetting('field_widget_edit') |
||
166 | ]; |
||
167 | |||
168 | $element['field_widget_remove'] = [ |
||
169 | '#title' => t('Display Remove button'), |
||
170 | '#type' => 'checkbox', |
||
171 | '#default_value' => $this->getSetting('field_widget_remove') |
||
172 | ]; |
||
173 | |||
174 | $element['open'] = [ |
||
175 | '#title' => t('Show widget details as open by default'), |
||
176 | '#type' => 'checkbox', |
||
177 | '#default_value' => $this->getSetting('open') |
||
178 | ]; |
||
179 | |||
180 | $element['field_widget_display_settings'] = [ |
||
181 | '#type' => 'fieldset', |
||
182 | '#title' => t('Entity display plugin configuration'), |
||
183 | '#tree' => TRUE, |
||
184 | '#prefix' => '<div id="' . $id . '">', |
||
185 | '#suffix' => '</div>', |
||
186 | ]; |
||
187 | |||
188 | if ($this->getSetting('field_widget_display')) { |
||
189 | $element['field_widget_display_settings'] += $this->fieldDisplayManager |
||
190 | ->createInstance( |
||
191 | $form_state->getValue( |
||
192 | ['fields', $this->fieldDefinition->getName(), 'settings_edit_form', 'settings', 'field_widget_display'], |
||
193 | $this->getSetting('field_widget_display') |
||
194 | ), |
||
195 | $form_state->getValue( |
||
196 | ['fields', $this->fieldDefinition->getName(), 'settings_edit_form', 'settings', 'field_widget_display_settings'], |
||
197 | $this->getSetting('field_widget_display_settings') |
||
198 | ) + ['entity_type' => $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type')] |
||
199 | ) |
||
200 | ->settingsForm($form, $form_state); |
||
201 | } |
||
202 | |||
203 | return $element; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Ajax callback that updates field widget display settings fieldset. |
||
208 | */ |
||
209 | public function updateSettingsAjax(array $form, FormStateInterface $form_state) { |
||
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function settingsSummary() { |
||
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { |
||
340 | |||
341 | /** |
||
342 | * {@inheritdoc} |
||
343 | */ |
||
344 | public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { |
||
353 | |||
354 | /** |
||
355 | * AJAX form callback. |
||
356 | */ |
||
357 | public static function updateWidgetCallback(array &$form, FormStateInterface $form_state) { |
||
372 | |||
373 | /** |
||
374 | * Submit callback for remove buttons. |
||
375 | */ |
||
376 | public static function removeItemSubmit(&$form, FormStateInterface $form_state) { |
||
400 | |||
401 | /** |
||
402 | * Builds the render array for displaying the current results. |
||
403 | * |
||
404 | * @param string $details_id |
||
405 | * The ID for the details element. |
||
406 | * @param string[] $field_parents |
||
407 | * Field parents. |
||
408 | * @param \Drupal\Core\Entity\ContentEntityInterface[] $entities |
||
409 | * |
||
410 | * @return array |
||
411 | * The render array for the current selection. |
||
412 | */ |
||
413 | protected function displayCurrentSelection($details_id, $field_parents, $entities) { |
||
468 | } |
||
469 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.