Complex classes like EntityEmbedDialog 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 EntityEmbedDialog, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class EntityEmbedDialog extends FormBase { |
||
| 34 | use EntityHelperTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The form builder. |
||
| 38 | * |
||
| 39 | * @var \Drupal\Core\Form\FormBuilderInterface |
||
| 40 | */ |
||
| 41 | protected $formBuilder; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The entity type manager service. |
||
| 45 | * |
||
| 46 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||
| 47 | */ |
||
| 48 | protected $entityTypeManager; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Event dispatcher service. |
||
| 52 | * |
||
| 53 | * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface |
||
| 54 | */ |
||
| 55 | protected $eventDispatcher; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The entity browser. |
||
| 59 | * |
||
| 60 | * @var \Drupal\entity_browser\EntityBrowserInterface |
||
| 61 | */ |
||
| 62 | protected $entityBrowser; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The entity browser settings from the entity embed button. |
||
| 66 | */ |
||
| 67 | protected $entityBrowserSettings = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Constructs a EntityEmbedDialog object. |
||
| 71 | * |
||
| 72 | * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $plugin_manager |
||
| 73 | * The Module Handler. |
||
| 74 | * @param \Drupal\Core\Form\FormBuilderInterface $form_builder |
||
| 75 | * The Form Builder. |
||
| 76 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
||
| 77 | * The entity type manager service. |
||
| 78 | * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
||
| 79 | * Event dispatcher service. |
||
| 80 | */ |
||
| 81 | public function __construct(EntityEmbedDisplayManager $plugin_manager, FormBuilderInterface $form_builder, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritdoc} |
||
| 90 | */ |
||
| 91 | public static function create(ContainerInterface $container) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | public function getFormId() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | * |
||
| 110 | * @param \Drupal\editor\EditorInterface $editor |
||
| 111 | * The editor to which this dialog corresponds. |
||
| 112 | * @param \Drupal\embed\EmbedButtonInterface $embed_button |
||
| 113 | * The URL button to which this dialog corresponds. |
||
| 114 | */ |
||
| 115 | public function buildForm(array $form, FormStateInterface $form_state, EditorInterface $editor = NULL, EmbedButtonInterface $embed_button = NULL) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Form constructor for the entity selection step. |
||
| 177 | * |
||
| 178 | * @param array $form |
||
| 179 | * An associative array containing the structure of the form. |
||
| 180 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
| 181 | * The current state of the form. |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | * The form structure. |
||
| 185 | */ |
||
| 186 | public function buildSelectStep(array &$form, FormStateInterface $form_state) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Form constructor for the entity review step. |
||
| 273 | * |
||
| 274 | * @param array $form |
||
| 275 | * An associative array containing the structure of the form. |
||
| 276 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
| 277 | * The current state of the form. |
||
| 278 | * |
||
| 279 | * @return array |
||
| 280 | * The form structure. |
||
| 281 | */ |
||
| 282 | public function buildReviewStep(array &$form, FormStateInterface $form_state) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Form constructor for the entity embedding step. |
||
| 327 | * |
||
| 328 | * @param array $form |
||
| 329 | * An associative array containing the structure of the form. |
||
| 330 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
| 331 | * The current state of the form. |
||
| 332 | * |
||
| 333 | * @return array |
||
| 334 | * The form structure. |
||
| 335 | */ |
||
| 336 | public function buildEmbedStep(array $form, FormStateInterface $form_state) { |
||
| 337 | $entity_element = $form_state->get('entity_element'); |
||
| 338 | /** @var \Drupal\embed\EmbedButtonInterface $embed_button */ |
||
| 339 | $embed_button = $form_state->get('embed_button'); |
||
| 340 | /** @var \Drupal\editor\EditorInterface $editor */ |
||
| 341 | $editor = $form_state->get('editor'); |
||
| 342 | $entity = $form_state->get('entity'); |
||
| 343 | $values = $form_state->getValues(); |
||
| 344 | |||
| 345 | $entity_label = ''; |
||
| 346 | try { |
||
| 347 | $entity_label = $entity->link(); |
||
| 348 | } |
||
| 349 | catch (\Exception $e) { |
||
| 350 | // Construct markup of the link to the entity manually if link() fails. |
||
| 351 | // @see https://www.drupal.org/node/2402533 |
||
| 352 | $entity_label = '<a href="' . $entity->url() . '">' . $entity->label() . '</a>'; |
||
| 353 | } |
||
| 354 | |||
| 355 | $form['entity'] = array( |
||
| 356 | '#type' => 'item', |
||
| 357 | '#title' => $this->t('Selected entity'), |
||
| 358 | '#markup' => $entity_label, |
||
| 359 | ); |
||
| 360 | $form['attributes']['data-entity-type'] = array( |
||
| 361 | '#type' => 'hidden', |
||
| 362 | '#value' => $entity_element['data-entity-type'], |
||
| 363 | ); |
||
| 364 | $form['attributes']['data-entity-id'] = array( |
||
| 365 | '#type' => 'hidden', |
||
| 366 | '#value' => $entity_element['data-entity-id'], |
||
| 367 | ); |
||
| 368 | $form['attributes']['data-entity-uuid'] = array( |
||
| 369 | '#type' => 'hidden', |
||
| 370 | '#value' => $entity_element['data-entity-uuid'], |
||
| 371 | ); |
||
| 372 | |||
| 373 | // Build the list of allowed Entity Embed Display plugins. |
||
| 374 | $display_plugin_options = $this->getDisplayPluginOptions($embed_button, $entity); |
||
| 375 | |||
| 376 | // If the currently selected display is not in the available options, |
||
| 377 | // use the first from the list instead. This can happen if an alter |
||
| 378 | // hook customizes the list based on the entity. |
||
| 379 | if (!isset($display_plugin_options[$entity_element['data-entity-embed-display']])) { |
||
| 380 | $entity_element['data-entity-embed-display'] = key($display_plugin_options); |
||
| 381 | } |
||
| 382 | |||
| 383 | // The default Entity Embed Display plugin has been deprecated by the |
||
| 384 | // rendered entity field formatter. |
||
| 385 | if ($entity_element['data-entity-embed-display'] === 'default') { |
||
| 386 | $entity_element['data-entity-embed-display'] = 'entity_reference:entity_reference_entity_view'; |
||
| 387 | } |
||
| 388 | |||
| 389 | $form['attributes']['data-entity-embed-display'] = array( |
||
| 390 | '#type' => 'select', |
||
| 391 | '#title' => $this->t('Display as'), |
||
| 392 | '#options' => $display_plugin_options, |
||
| 393 | '#default_value' => $entity_element['data-entity-embed-display'], |
||
| 394 | '#required' => TRUE, |
||
| 395 | '#ajax' => array( |
||
| 396 | 'callback' => '::updatePluginConfigurationForm', |
||
| 397 | 'wrapper' => 'data-entity-embed-settings-wrapper', |
||
| 398 | 'effect' => 'fade', |
||
| 399 | ), |
||
| 400 | // Hide the selection if only one option is available. |
||
| 401 | '#access' => count($display_plugin_options) > 1, |
||
| 402 | ); |
||
| 403 | $form['attributes']['data-entity-embed-settings'] = array( |
||
| 404 | '#type' => 'container', |
||
| 405 | '#prefix' => '<div id="data-entity-embed-settings-wrapper">', |
||
| 406 | '#suffix' => '</div>', |
||
| 407 | ); |
||
| 408 | $form['attributes']['data-embed-button'] = array( |
||
| 409 | '#type' => 'value', |
||
| 410 | '#value' => $embed_button->id(), |
||
| 411 | ); |
||
| 412 | $form['attributes']['data-entity-label'] = array( |
||
| 413 | '#type' => 'value', |
||
| 414 | '#value' => $embed_button->label(), |
||
| 415 | ); |
||
| 416 | $plugin_id = !empty($values['attributes']['data-entity-embed-display']) ? $values['attributes']['data-entity-embed-display'] : $entity_element['data-entity-embed-display']; |
||
| 417 | if (!empty($plugin_id)) { |
||
| 418 | if (is_string($entity_element['data-entity-embed-settings'])) { |
||
| 419 | $entity_element['data-entity-embed-settings'] = Json::decode($entity_element['data-entity-embed-settings']); |
||
| 420 | } |
||
| 421 | $display = $this->displayPluginManager()->createInstance($plugin_id, $entity_element['data-entity-embed-settings']); |
||
| 422 | $display->setContextValue('entity', $entity); |
||
| 423 | $display->setAttributes($entity_element); |
||
| 424 | $form['attributes']['data-entity-embed-settings'] += $display->buildConfigurationForm($form, $form_state); |
||
| 425 | } |
||
| 426 | |||
| 427 | // When Drupal core's filter_align is being used, the text editor may |
||
| 428 | // offer the ability to change the alignment. |
||
| 429 | if (isset($entity_element['data-align']) && $editor->getFilterFormat()->filters('filter_align')->status) { |
||
| 430 | $form['attributes']['data-align'] = array( |
||
| 431 | '#title' => $this->t('Align'), |
||
| 432 | '#type' => 'radios', |
||
| 433 | '#options' => array( |
||
| 434 | 'none' => $this->t('None'), |
||
| 435 | 'left' => $this->t('Left'), |
||
| 436 | 'center' => $this->t('Center'), |
||
| 437 | 'right' => $this->t('Right'), |
||
| 438 | ), |
||
| 439 | '#default_value' => $entity_element['data-align'] === '' ? 'none' : $entity_element['data-align'], |
||
| 440 | '#wrapper_attributes' => array('class' => array('container-inline')), |
||
| 441 | '#attributes' => array('class' => array('container-inline')), |
||
| 442 | ); |
||
| 443 | } |
||
| 444 | |||
| 445 | // When Drupal core's filter_caption is being used, the text editor may |
||
| 446 | // offer the ability to add a caption. |
||
| 447 | if (isset($entity_element['data-caption']) && $editor->getFilterFormat()->filters('filter_caption')->status) { |
||
| 448 | $form['attributes']['data-caption'] = array( |
||
| 449 | '#title' => $this->t('Caption'), |
||
| 450 | '#type' => 'textfield', |
||
| 451 | '#default_value' => Html::decodeEntities($entity_element['data-caption']), |
||
| 452 | '#element_validate' => array('::escapeValue'), |
||
| 453 | ); |
||
| 454 | } |
||
| 455 | |||
| 456 | $form['actions'] = array( |
||
| 457 | '#type' => 'actions', |
||
| 458 | ); |
||
| 459 | $form['actions']['back'] = array( |
||
| 460 | '#type' => 'submit', |
||
| 461 | '#value' => $this->t('Back'), |
||
| 462 | // No regular submit-handler. This form only works via JavaScript. |
||
| 463 | '#submit' => array(), |
||
| 464 | '#ajax' => array( |
||
| 465 | 'callback' => !empty($this->entityBrowserSettings['display_review']) ? '::submitAndShowReview' : '::submitAndShowSelect', |
||
| 466 | 'event' => 'click', |
||
| 467 | ), |
||
| 468 | ); |
||
| 469 | $form['actions']['save_modal'] = array( |
||
| 470 | '#type' => 'submit', |
||
| 471 | '#value' => $this->t('Embed'), |
||
| 472 | '#button_type' => 'primary', |
||
| 473 | // No regular submit-handler. This form only works via JavaScript. |
||
| 474 | '#submit' => array(), |
||
| 475 | '#ajax' => array( |
||
| 476 | 'callback' => '::submitEmbedStep', |
||
| 477 | 'event' => 'click', |
||
| 478 | ), |
||
| 479 | ); |
||
| 480 | |||
| 481 | return $form; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * {@inheritdoc} |
||
| 486 | */ |
||
| 487 | public function validateForm(array &$form, FormStateInterface $form_state) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Form validation handler for the entity selection step. |
||
| 500 | * |
||
| 501 | * @param array $form |
||
| 502 | * An associative array containing the structure of the form. |
||
| 503 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
| 504 | * The current state of the form. |
||
| 505 | */ |
||
| 506 | public function validateSelectStep(array $form, FormStateInterface $form_state) { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Form validation handler for the entity embedding step. |
||
| 548 | * |
||
| 549 | * @param array $form |
||
| 550 | * An associative array containing the structure of the form. |
||
| 551 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
| 552 | * The current state of the form. |
||
| 553 | */ |
||
| 554 | public function validateEmbedStep(array $form, FormStateInterface $form_state) { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * {@inheritdoc} |
||
| 568 | */ |
||
| 569 | public function submitForm(array &$form, FormStateInterface $form_state) {} |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Form submission handler to update the plugin configuration form. |
||
| 573 | * |
||
| 574 | * @param array $form |
||
| 575 | * The form array. |
||
| 576 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
| 577 | * The form state. |
||
| 578 | */ |
||
| 579 | public function updatePluginConfigurationForm(array &$form, FormStateInterface $form_state) { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Form submission handler to to another step of the form. |
||
| 585 | * |
||
| 586 | * @param array $form |
||
| 587 | * The form array. |
||
| 588 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
| 589 | * The form state. |
||
| 590 | * |
||
| 591 | * @return \Drupal\Core\Ajax\AjaxResponse |
||
| 592 | * The ajax response. |
||
| 593 | */ |
||
| 594 | public function submitStep(array &$form, FormStateInterface $form_state, $step) { |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Form submission handler for the entity selection step. |
||
| 608 | * |
||
| 609 | * On success will send the user to the next step of the form to select the |
||
| 610 | * embed display settings. On form errors, this will rebuild the form and |
||
| 611 | * display the error messages. |
||
| 612 | * |
||
| 613 | * @param array $form |
||
| 614 | * The form array. |
||
| 615 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
| 616 | * The form state. |
||
| 617 | * |
||
| 618 | * @return \Drupal\Core\Ajax\AjaxResponse |
||
| 619 | * The ajax response. |
||
| 620 | */ |
||
| 621 | public function submitSelectStep(array &$form, FormStateInterface $form_state) { |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Submit and show select step after submit. |
||
| 646 | * |
||
| 647 | * @param array $form |
||
| 648 | * The form array. |
||
| 649 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
| 650 | * The form state. |
||
| 651 | * |
||
| 652 | * @return \Drupal\Core\Ajax\AjaxResponse |
||
| 653 | * The ajax response. |
||
| 654 | */ |
||
| 655 | public function submitAndShowSelect(array &$form, FormStateInterface $form_state) { |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Submit and show review step after submit. |
||
| 661 | * |
||
| 662 | * @param array $form |
||
| 663 | * The form array. |
||
| 664 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
| 665 | * The form state. |
||
| 666 | * |
||
| 667 | * @return \Drupal\Core\Ajax\AjaxResponse |
||
| 668 | * The ajax response. |
||
| 669 | */ |
||
| 670 | public function submitAndShowReview(array &$form, FormStateInterface $form_state) { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Submit and show embed step after submit. |
||
| 676 | * |
||
| 677 | * @param array $form |
||
| 678 | * The form array. |
||
| 679 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
| 680 | * The form state. |
||
| 681 | * |
||
| 682 | * @return \Drupal\Core\Ajax\AjaxResponse |
||
| 683 | * The ajax response. |
||
| 684 | */ |
||
| 685 | public function submitAndShowEmbed(array $form, FormStateInterface $form_state) { |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Form submission handler for the entity embedding step. |
||
| 691 | * |
||
| 692 | * On success this will submit the command to save the embedded entity with |
||
| 693 | * the configured display settings to the WYSIWYG element, and then close the |
||
| 694 | * modal dialog. On form errors, this will rebuild the form and display the |
||
| 695 | * error messages. |
||
| 696 | * |
||
| 697 | * @param array $form |
||
| 698 | * An associative array containing the structure of the form. |
||
| 699 | * @param FormStateInterface $form_state |
||
| 700 | * An associative array containing the current state of the form. |
||
| 701 | * |
||
| 702 | * @return \Drupal\Core\Ajax\AjaxResponse |
||
| 703 | * The ajax response. |
||
| 704 | */ |
||
| 705 | public function submitEmbedStep(array &$form, FormStateInterface $form_state) { |
||
| 706 | $response = new AjaxResponse(); |
||
| 707 | |||
| 708 | // Submit configuration form the selected Entity Embed Display plugin. |
||
| 709 | $entity_element = $form_state->getValue('attributes'); |
||
| 710 | $entity = $this->loadEntity($entity_element['data-entity-type'], $entity_element['data-entity-uuid']); |
||
| 711 | $plugin_id = $entity_element['data-entity-embed-display']; |
||
| 712 | $plugin_settings = $entity_element['data-entity-embed-settings'] ?: array(); |
||
| 713 | $display = $this->displayPluginManager()->createInstance($plugin_id, $plugin_settings); |
||
| 714 | $display->setContextValue('entity', $entity); |
||
| 715 | $display->setAttributes($entity_element); |
||
| 716 | $display->submitConfigurationForm($form, $form_state); |
||
| 717 | |||
| 718 | $values = $form_state->getValues(); |
||
| 719 | // Display errors in form, if any. |
||
| 720 | if ($form_state->hasAnyErrors()) { |
||
| 721 | unset($form['#prefix'], $form['#suffix']); |
||
| 722 | $form['status_messages'] = array( |
||
| 723 | '#type' => 'status_messages', |
||
| 724 | '#weight' => -10, |
||
| 725 | ); |
||
| 726 | $response->addCommand(new HtmlCommand('#entity-embed-dialog-form', $form)); |
||
| 727 | } |
||
| 728 | else { |
||
| 729 | // Serialize entity embed settings to JSON string. |
||
| 730 | if (!empty($values['attributes']['data-entity-embed-settings'])) { |
||
| 731 | $values['attributes']['data-entity-embed-settings'] = Json::encode($values['attributes']['data-entity-embed-settings']); |
||
| 732 | } |
||
| 733 | |||
| 734 | // Allow other modules to alter the values before getting submitted to the WYSIWYG. |
||
| 735 | $this->moduleHandler()->alter('entity_embed_values', $values, $entity, $display, $form_state); |
||
| 736 | |||
| 737 | $response->addCommand(new EditorDialogSave($values)); |
||
| 738 | $response->addCommand(new CloseModalDialogCommand()); |
||
| 739 | } |
||
| 740 | |||
| 741 | return $response; |
||
| 742 | } |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Form element validation handler; Escapes the value an element. |
||
| 746 | * |
||
| 747 | * This should be used for any element in the embed form which may contain |
||
| 748 | * HTML that should be serialized as an attribute element on the embed. |
||
| 749 | */ |
||
| 750 | public static function escapeValue($element, FormStateInterface $form_state) { |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Returns the allowed Entity Embed Display plugins given an embed button and |
||
| 758 | * an entity. |
||
| 759 | * |
||
| 760 | * @param \Drupal\embed\EmbedButtonInterface $embed_button |
||
| 761 | * The embed button. |
||
| 762 | * @param \Drupal\Core\Entity\EntityInterface $entity |
||
| 763 | * The entity. |
||
| 764 | * |
||
| 765 | * @return array |
||
| 766 | * List of allowed Entity Embed Display plugins. |
||
| 767 | */ |
||
| 768 | public function getDisplayPluginOptions(EmbedButtonInterface $embed_button, EntityInterface $entity) { |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Registers JS callback that gets entities from entity browser and updates |
||
| 781 | * form values accordingly. |
||
| 782 | */ |
||
| 783 | public function registerJSCallback(RegisterJSCallbacks $event) { |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Load the current entity browser and its settings from the form state. |
||
| 791 | * |
||
| 792 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
||
| 793 | */ |
||
| 794 | protected function loadEntityBrowser(FormStateInterface $form_state) { |
||
| 806 | |||
| 807 | } |
||
| 808 |