class UpgradeRectorForm in Upgrade Rector 8
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\upgrade_rector\Form\UpgradeRectorForm
Expanded class hierarchy of UpgradeRectorForm
1 string reference to 'UpgradeRectorForm'
File
- src/
Form/ UpgradeRectorForm.php, line 16
Namespace
Drupal\upgrade_rector\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UpgradeRectorForm:: |
protected | property | The project collector service. | |
UpgradeRectorForm:: |
protected | property | Rector data processor. | |
UpgradeRectorForm:: |
protected | property | Rector result storage. | |
UpgradeRectorForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
UpgradeRectorForm:: |
private | function | Builds a list and status summary of projects. | |
UpgradeRectorForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
UpgradeRectorForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
UpgradeRectorForm:: |
public | function | Form submission handler. | |
UpgradeRectorForm:: |
public | function | Form submission handler. | |
UpgradeRectorForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
UpgradeRectorForm:: |
public | function | Form submission handler. | |
UpgradeRectorForm:: |
public | function | Constructs a \Drupal\upgrade_status_rector\UpgradeStatusRectorForm. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |