You are here

class UpgradeRectorForm in Upgrade Rector 8

Hierarchy

Expanded class hierarchy of UpgradeRectorForm

1 string reference to 'UpgradeRectorForm'
upgrade_rector.routing.yml in ./upgrade_rector.routing.yml
upgrade_rector.routing.yml

File

src/Form/UpgradeRectorForm.php, line 16

Namespace

Drupal\upgrade_rector\Form
View source
class UpgradeRectorForm extends FormBase {

  /**
   * The project collector service.
   *
   * @var \Drupal\upgrade_rector\ProjectCollector
   */
  protected $projectCollector;

  /**
   * Rector result storage.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
  protected $rectorResults;

  /**
   * Rector data processor.
   *
   * @var \Drupal\upgrade_rector\RectorProcessor
   */
  protected $rectorProcessor;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('upgrade_rector.project_collector'), $container
      ->get('keyvalue'), $container
      ->get('upgrade_rector.rector_processor'));
  }

  /**
   * Constructs a \Drupal\upgrade_status_rector\UpgradeStatusRectorForm.
   *
   * @param \Drupal\upgrade_rector\ProjectCollector $project_collector
   *   The project collector service.
   * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
   *   The key/value factory.
   * @param \Drupal\upgrade_rector\RectorProcessor $rector_processor
   *   The rector processor.
   */
  public function __construct(ProjectCollector $project_collector, KeyValueFactoryInterface $key_value_factory, RectorProcessor $rector_processor) {
    $this->projectCollector = $project_collector;
    $this->rectorResults = $key_value_factory
      ->get('upgrade_status_rector_results');
    $this->rectorProcessor = $rector_processor;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'drupal_upgrade_rector_form';
  }

  /**
   * Form constructor.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   The form structure.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['#attached']['library'][] = 'upgrade_rector/upgrade_rector.admin';

    // Gather project list grouped by custom and contrib projects.
    $projects = $this->projectCollector
      ->collectProjects();

    // List custom project status first.
    $custom = [
      '#type' => 'markup',
      '#markup' => '<br /><strong>' . $this
        ->t('No custom projects found.') . '</strong>',
    ];
    if (count($projects['custom'])) {
      $custom = $this
        ->buildProjectList($projects['custom'], 'custom');
    }
    $form['custom'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Custom projects'),
      '#description' => $this
        ->t('Custom code is specific to your site, and must be upgraded manually. <a href=":upgrade">Read more about how developers can upgrade their code to Drupal 9</a>.', [
        ':upgrade' => 'https://www.drupal.org/docs/9/how-drupal-9-is-made-and-what-is-included/how-and-why-we-deprecate-on-the-way-to-drupal-9',
      ]),
      '#open' => TRUE,
      '#attributes' => [
        'class' => [
          'upgrade-rector-summary',
        ],
      ],
      'data' => $custom,
      '#tree' => TRUE,
    ];

    // List contrib project status second.
    $contrib = [
      '#type' => 'markup',
      '#markup' => '<br /><strong>' . $this
        ->t('No contributed projects found.') . '</strong>',
    ];
    if (count($projects['contrib'])) {
      $contrib = $this
        ->buildProjectList($projects['contrib'], 'contrib');
    }
    $form['contrib'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Contributed projects'),
      '#description' => $this
        ->t('Contributed code is available from drupal.org. Problems here may be partially resolved by updating to the latest version. <a href=":update">Read more about how to update contributed projects</a>.', [
        ':update' => 'https://www.drupal.org/docs/8/update/update-modules',
      ]),
      '#open' => TRUE,
      '#attributes' => [
        'class' => [
          'upgrade-rector-summary',
        ],
      ],
      'data' => $contrib,
      '#tree' => TRUE,
    ];
    return $form;
  }

  /**
   * Builds a list and status summary of projects.
   *
   * @param \Drupal\Core\Extension\Extension[] $projects
   *   Array of extensions representing projects.
   * @param string $category
   *   One of 'custom' or 'contrib'. Presenting messages may be different for each.
   *
   * @return array
   *   Build array.
   */
  private function buildProjectList(array $projects, string $category) {
    $list = $form = [];
    foreach ($projects as $name => $extension) {
      $info = $extension->info;
      $label = $info['name'] . (!empty($info['version']) ? ' ' . $info['version'] : '');
      $list[$name] = $label;
      if ($results = $this->rectorResults
        ->get($extension
        ->getName())) {
        $form[$name] = $this->rectorProcessor
          ->formatResults($results, $extension, $category) + [
          '#type' => 'details',
          '#closed' => TRUE,
        ];
      }
    }
    $form['project'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Select project'),
      '#options' => $list,
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#name' => 'rector_' . $category,
      '#value' => $this
        ->t('Run rector'),
      '#button_type' => 'primary',
      '#submit' => [
        [
          $this,
          'submit' . ucfirst($category) . 'Rector',
        ],
      ],
    ];
    return $form;
  }

  /**
   * Form submission handler.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function submitContribRector(array &$form, FormStateInterface $form_state) {
    $projects = $this->projectCollector
      ->collectProjects();
    $this
      ->submitRector($projects['contrib'][$form_state
      ->getValue([
      'contrib',
      'data',
      'project',
    ])]);
  }

  /**
   * Form submission handler.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function submitCustomRector(array &$form, FormStateInterface $form_state) {
    $projects = $this->projectCollector
      ->collectProjects();
    $this
      ->submitRector($projects['custom'][$form_state
      ->getValue([
      'custom',
      'data',
      'project',
    ])]);
  }

  /**
   * Form submission handler.
   *
   * @param \Drupal\Core\Extension\Extension $extension
   *   Selected extension.
   */
  public function submitRector(Extension $extension) {
    $info = $extension->info;
    $label = $info['name'] . (!empty($info['version']) ? ' ' . $info['version'] : '');
    if (\Drupal::service('upgrade_rector.rector_processor')
      ->runRector($extension)) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Parsing @project was successful.', [
        '@project' => $label,
      ]));
    }
    else {
      $this
        ->messenger()
        ->addError($this
        ->t('Error while parsing @project.', [
        '@project' => $label,
      ]));
    }
  }

  /**
   * Form submission handler.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UpgradeRectorForm::$projectCollector protected property The project collector service.
UpgradeRectorForm::$rectorProcessor protected property Rector data processor.
UpgradeRectorForm::$rectorResults protected property Rector result storage.
UpgradeRectorForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
UpgradeRectorForm::buildProjectList private function Builds a list and status summary of projects.
UpgradeRectorForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
UpgradeRectorForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
UpgradeRectorForm::submitContribRector public function Form submission handler.
UpgradeRectorForm::submitCustomRector public function Form submission handler.
UpgradeRectorForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
UpgradeRectorForm::submitRector public function Form submission handler.
UpgradeRectorForm::__construct public function Constructs a \Drupal\upgrade_status_rector\UpgradeStatusRectorForm.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.