class PoolAssignmentForm in CMS Content Sync 8
Same name and namespace in other branches
- 2.1.x src/Form/PoolAssignmentForm.php \Drupal\cms_content_sync\Form\PoolAssignmentForm
- 2.0.x src/Form/PoolAssignmentForm.php \Drupal\cms_content_sync\Form\PoolAssignmentForm
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\cms_content_sync\Form\PoolAssignmentForm
Expanded class hierarchy of PoolAssignmentForm
1 string reference to 'PoolAssignmentForm'
File
- src/
Form/ PoolAssignmentForm.php, line 12
Namespace
Drupal\cms_content_sync\FormView source
class PoolAssignmentForm extends FormBase {
public const STEP_SELECT_FLOW = 'flow';
public const STEP_SELECT_POOL = 'pool';
public const STEP_SELECT_ASSIGNMENT = 'assignment';
protected $step;
public function __construct() {
$this->step = self::STEP_SELECT_FLOW;
}
public function getFormId() {
return 'content_sync_pool_assignment_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['description'] = [
'#type' => 'item',
'#markup' => $this
->t('This form allows you to change a pool assignment within a Flow. You can change per Pool per Flow whether the assignment should be Allow, Force or Forbid across all entity types to make changing pool assignments faster than having to go through every entity type individually via the Flow edit form.'),
];
$form['wrapper'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'form-wrapper',
],
];
$flow_id = $form_state
->getValue('flow');
$pool_id = $form_state
->getValue('pool');
$next_step = null;
$flows = Flow::getAll(false);
$options = [];
foreach ($flows as $flow) {
if (Flow::TYPE_BOTH === $flow
->getType()) {
continue;
}
$options[$flow->id] = $flow->name;
}
$form['wrapper']['flow'] = [
'#type' => 'select',
'#options' => $options,
'#title' => $this
->t('Flow'),
'#required' => true,
'#default_value' => $flow_id,
'#disabled' => self::STEP_SELECT_FLOW !== $this->step,
'#description' => $this
->t('Please note that this only works for Flows that are either pulling or pushing but not both.'),
];
if (self::STEP_SELECT_FLOW === $this->step) {
$next_step = self::STEP_SELECT_POOL;
}
else {
$pools = Pool::getAll();
$options = [];
foreach ($pools as $pool) {
$options[$pool->id] = $pool->label;
}
$form['wrapper']['pool'] = [
'#type' => 'select',
'#options' => $options,
'#title' => $this
->t('Pool'),
'#required' => true,
'#default_value' => $pool_id,
'#disabled' => self::STEP_SELECT_POOL !== $this->step,
];
if (self::STEP_SELECT_POOL === $this->step) {
$next_step = self::STEP_SELECT_ASSIGNMENT;
}
else {
$flow = $flows[$flow_id];
$type = $flow
->getType();
$pool_index = Flow::TYPE_PUSH === $type ? 'export_pools' : 'import_pools';
$usages = [];
foreach ($flow->sync_entities as $id => $config) {
if (!isset($config[$pool_index][$pool_id])) {
continue;
}
$usage = $config[$pool_index][$pool_id];
if (in_array($usage, $usages)) {
continue;
}
$usages[] = $usage;
}
$options = [
Pool::POOL_USAGE_ALLOW => 'allow',
Pool::POOL_USAGE_FORCE => 'force',
Pool::POOL_USAGE_FORBID => 'forbid',
];
if (!count($usages)) {
$empty = 'Currently: Unassigned';
}
elseif (1 === count($usages)) {
$empty = 'Currently: ' . $usages[0];
// No need to apply the same value.
unset($options[$usages[0]]);
}
else {
$empty = 'Currently: mixed (' . implode(', ', $usages) . ')';
}
$form['wrapper']['assignment'] = [
'#empty_option' => $empty,
'#type' => 'select',
'#options' => $options,
'#title' => $this
->t('Assignment (' . $type . ')'),
'#required' => true,
'#default_value' => null,
];
}
}
if ($next_step) {
$form['wrapper']['actions'] = [
'#type' => 'actions',
];
$form['wrapper']['actions']['continue'] = [
'#type' => 'submit',
'#next_step' => $next_step,
'#value' => $this
->t('Continue'),
'#ajax' => [
'callback' => [
$this,
'loadStep',
],
'wrapper' => 'form-wrapper',
'effect' => 'fade',
],
];
}
else {
$form['wrapper']['actions'] = [
'#type' => 'actions',
];
$form['wrapper']['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Change assignment'),
'#submit' => [
'::submitValues',
],
];
}
return $form;
}
public function loadStep(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand(new HtmlCommand('#form-wrapper', $form['wrapper']));
return $response;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
if (isset($triggering_element['#next_step'])) {
$this->step = $triggering_element['#next_step'];
$form_state
->setRebuild(true);
}
}
public function submitValues(array &$form, FormStateInterface $form_state) {
$flow_id = $form_state
->getValue('flow');
$pool_id = $form_state
->getValue('pool');
$assignment = $form_state
->getValue('assignment');
$flow = Flow::getAll(false)[$flow_id];
$type = $flow
->getType();
$pool_index = Flow::TYPE_PUSH === $type ? 'export_pools' : 'import_pools';
foreach ($flow->sync_entities as &$config) {
if (!isset($config[$pool_index])) {
continue;
}
$config[$pool_index][$pool_id] = $assignment;
}
$flow
->save();
\Drupal::messenger()
->addStatus($this
->t('Updated pool assignment and saved the Flow.'));
\Drupal::messenger()
->addWarning($this
->t('Please export the Flow to apply the changes.'));
}
}
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:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
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. | |
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. | |
PoolAssignmentForm:: |
protected | property | ||
PoolAssignmentForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
PoolAssignmentForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
PoolAssignmentForm:: |
public | function | ||
PoolAssignmentForm:: |
public | constant | ||
PoolAssignmentForm:: |
public | constant | ||
PoolAssignmentForm:: |
public | constant | ||
PoolAssignmentForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
PoolAssignmentForm:: |
public | function | ||
PoolAssignmentForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
PoolAssignmentForm:: |
public | function | ||
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. | |
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. |