class ManageConditions
Same name in other branches
- 8.x-3.x src/Form/ManageConditions.php \Drupal\ctools\Form\ManageConditions
Hierarchy
- class \Drupal\Core\Form\FormBase implements \Drupal\Core\Form\FormInterface, \Drupal\Core\DependencyInjection\ContainerInjectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Logger\LoggerChannelTrait, \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\Routing\RedirectDestinationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
- class \Drupal\ctools\Form\ManageConditions extends \Drupal\Core\Form\FormBase
Expanded class hierarchy of ManageConditions
File
-
src/
Form/ ManageConditions.php, line 18
Namespace
Drupal\ctools\FormView source
abstract class ManageConditions extends FormBase {
/**
* @var \Drupal\Core\Condition\ConditionManager
*/
protected $manager;
/**
* The builder of form.
*
* @var \Drupal\Core\Form\FormBuilder
*/
protected $formBuilder;
/**
* @var string
*/
protected $machine_name;
/**
*
*/
public static function create(ContainerInterface $container) {
return new static($container->get('plugin.manager.condition'), $container->get('form_builder'));
}
/**
*
*/
public function __construct(PluginManagerInterface $manager, FormBuilderInterface $form_builder) {
$this->manager = $manager;
$this->formBuilder = $form_builder;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'ctools_manage_conditions_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$cached_values = $form_state->getTemporaryValue('wizard');
$this->machine_name = $cached_values['id'];
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$options = [];
$contexts = $this->getContexts($cached_values);
foreach ($this->manager
->getDefinitionsForContexts($contexts) as $plugin_id => $definition) {
$options[$plugin_id] = (string) $definition['label'];
}
$form['items'] = [
'#type' => 'markup',
'#prefix' => '<div id="configured-conditions">',
'#suffix' => '</div>',
'#theme' => 'table',
'#header' => [
$this->t('Plugin Id'),
$this->t('Summary'),
$this->t('Operations'),
],
'#rows' => $this->renderRows($cached_values),
'#empty' => $this->t('No required conditions have been configured.'),
];
$form['conditions'] = [
'#type' => 'select',
'#options' => $options,
];
$form['add'] = [
'#type' => 'submit',
'#name' => 'add',
'#value' => $this->t('Add Condition'),
'#ajax' => [
'callback' => [
$this,
'add',
],
'event' => 'click',
],
'#submit' => [
'callback' => [
$this,
'submitForm',
],
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state->getTemporaryValue('wizard');
[
,
$route_parameters,
] = $this->getOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('conditions'));
$form_state->setRedirect($this->getAddRoute($cached_values), $route_parameters);
}
/**
*
*/
public function add(array &$form, FormStateInterface $form_state) {
$condition = $form_state->getValue('conditions');
$content = $this->formBuilder
->getForm($this->getConditionClass(), $condition, $this->getTempstoreId(), $this->machine_name);
$content['#attached']['library'][] = 'core/drupal.dialog.ajax';
$cached_values = $form_state->getTemporaryValue('wizard');
[
,
$route_parameters,
] = $this->getOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('conditions'));
$route_name = $this->getAddRoute($cached_values);
$route_options = [
'query' => [
FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
],
];
$url = Url::fromRoute($route_name, $route_parameters, $route_options);
$content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = $url->toString();
$response = new AjaxResponse();
$response->addCommand(new OpenModalDialogCommand($this->t('Configure Required Context'), $content, [
'width' => '700',
]));
return $response;
}
/**
* @param $cached_values
*
* @return array
*/
public function renderRows($cached_values) {
$configured_conditions = [];
foreach ($this->getConditions($cached_values) as $row => $condition) {
/** @var \Drupal\Core\Condition\ConditionInterface $instance */
$instance = $this->manager
->createInstance($condition['id'], $condition);
[
$route_name,
$route_parameters,
] = $this->getOperationsRouteInfo($cached_values, $cached_values['id'], $row);
$build = [
'#type' => 'operations',
'#links' => $this->getOperations($route_name, $route_parameters),
];
$configured_conditions[] = [
$instance->getPluginId(),
$instance->summary(),
'operations' => [
'data' => $build,
],
];
}
return $configured_conditions;
}
/**
*
*/
protected function getOperations($route_name_base, array $route_parameters = []) {
$operations['edit'] = [
'title' => $this->t('Edit'),
'url' => new Url($route_name_base . '.edit', $route_parameters),
'weight' => 10,
'attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
];
$route_parameters['id'] = $route_parameters['condition'];
$operations['delete'] = [
'title' => $this->t('Delete'),
'url' => new Url($route_name_base . '.delete', $route_parameters),
'weight' => 100,
'attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
];
return $operations;
}
/**
* Return a subclass of '\Drupal\ctools\Form\ConditionConfigure'.
*
* The ConditionConfigure class is designed to be subclassed with custom
* route information to control the modal/redirect needs of your use case.
*
* @return string
*/
protected abstract function getConditionClass();
/**
* The route to which condition 'add' actions should submit.
*
* @param mixed $cached_values
*
* @return string
*/
protected abstract function getAddRoute($cached_values);
/**
* Provide the tempstore id for your specified use case.
*
* @return string
*/
protected abstract function getTempstoreId();
/**
* Document the route name and parameters for edit/delete context operations.
*
* The route name returned from this method is used as a "base" to which
* ".edit" and ".delete" are appended in the getOperations() method.
* Subclassing '\Drupal\ctools\Form\ConditionConfigure' and
* '\Drupal\ctools\Form\ConditionDelete' should set you up for using this
* approach quite seamlessly.
*
* @param mixed $cached_values
*
* @param string $machine_name
*
* @param string $row
*
* @return array
* In the format of
* return ['route.base.name', ['machine_name' => $machine_name, 'context' => $row]];
*/
protected abstract function getOperationsRouteInfo($cached_values, $machine_name, $row);
/**
* Custom logic for retrieving the conditions array from cached_values.
*
* @param $cached_values
*
* @return array
*/
protected abstract function getConditions($cached_values);
/**
* Custom logic for retrieving the contexts array from cached_values.
*
* @param $cached_values
*
* @return \Drupal\Core\Plugin\Context\ContextInterface[]
*/
protected abstract function getContexts($cached_values);
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
DependencySerializationTrait::$_entityStorages | protected | property | |||
DependencySerializationTrait::$_serviceIds | protected | property | |||
DependencySerializationTrait::__sleep | public | function | 1 | ||
DependencySerializationTrait::__wakeup | public | function | 2 | ||
FormBase::$configFactory | protected | property | The config factory. | 3 | |
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. | 3 | |
FormBase::container | private | function | Returns the service container. | ||
FormBase::currentUser | protected | function | Gets the current user. | 2 | |
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. | ||
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 | 57 |
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. | ||
ManageConditions::$formBuilder | protected | property | The builder of form. | ||
ManageConditions::$machine_name | protected | property | |||
ManageConditions::$manager | protected | property | |||
ManageConditions::add | public | function | |||
ManageConditions::buildForm | public | function | Form constructor. | Overrides FormInterface::buildForm | |
ManageConditions::create | public static | function | Instantiates a new instance of this class. | Overrides FormBase::create | |
ManageConditions::getAddRoute | abstract protected | function | The route to which condition 'add' actions should submit. | ||
ManageConditions::getConditionClass | abstract protected | function | Return a subclass of '\Drupal\ctools\Form\ConditionConfigure'. | ||
ManageConditions::getConditions | abstract protected | function | Custom logic for retrieving the conditions array from cached_values. | ||
ManageConditions::getContexts | abstract protected | function | Custom logic for retrieving the contexts array from cached_values. | ||
ManageConditions::getFormId | public | function | Returns a unique string identifying the form. | Overrides FormInterface::getFormId | |
ManageConditions::getOperations | protected | function | |||
ManageConditions::getOperationsRouteInfo | abstract protected | function | Document the route name and parameters for edit/delete context operations. | ||
ManageConditions::getTempstoreId | abstract protected | function | Provide the tempstore id for your specified use case. | ||
ManageConditions::renderRows | public | function | |||
ManageConditions::submitForm | public | function | Form submission handler. | Overrides FormInterface::submitForm | |
ManageConditions::__construct | public | function | |||
MessengerTrait::$messenger | protected | property | The messenger. | 16 | |
MessengerTrait::messenger | public | function | Gets the messenger. | 16 | |
MessengerTrait::setMessenger | public | function | Sets the messenger. | ||
RedirectDestinationTrait::$redirectDestination | protected | property | The redirect destination service. | 2 | |
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. | 3 | |
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. |