class ContextListBuilder in Context 8
Same name and namespace in other branches
- 8.4 modules/context_ui/src/ContextListBuilder.php \Drupal\context_ui\ContextListBuilder
- 8.0 modules/context_ui/src/ContextListBuilder.php \Drupal\context_ui\ContextListBuilder
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityListBuilder implements EntityHandlerInterface, EntityListBuilderInterface uses MessengerTrait, RedirectDestinationTrait
- class \Drupal\Core\Config\Entity\ConfigEntityListBuilder
- class \Drupal\context_ui\ContextListBuilder implements FormInterface uses AjaxFormTrait
- class \Drupal\Core\Config\Entity\ConfigEntityListBuilder
- class \Drupal\Core\Entity\EntityListBuilder implements EntityHandlerInterface, EntityListBuilderInterface uses MessengerTrait, RedirectDestinationTrait
Expanded class hierarchy of ContextListBuilder
File
- modules/
context_ui/ src/ ContextListBuilder.php, line 18
Namespace
Drupal\context_uiView source
class ContextListBuilder extends ConfigEntityListBuilder implements FormInterface {
use AjaxFormTrait;
/**
* The Context modules context manager.
*
* @var ContextManager
*/
protected $contextManager;
/**
* @var FormBuilderInterface
*/
protected $formBuilder;
/**
* Constructs a new ContextListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
*
* @param \Drupal\context\ContextManager $contextManager
* The Context module context manager.
*
* @param \Drupal\Core\Form\FormBuilderInterface $formBuilder
* The Drupal form builder.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, ContextManager $contextManager, FormBuilderInterface $formBuilder) {
parent::__construct($entity_type, $storage);
$this->contextManager = $contextManager;
$this->formBuilder = $formBuilder;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity.manager')
->getStorage($entity_type
->id()), $container
->get('context.manager'), $container
->get('form_builder'));
}
/**
* Use a form instead of the entity list builder to display contexts.
*
* {@inheritdoc}
*/
public function render() {
return $this->formBuilder
->getForm($this);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'context_ui_admin_display_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$groups = $this->contextManager
->getContextsByGroup();
$form['contexts'] = array(
'#type' => 'table',
'#header' => array(
$this
->t('Context'),
$this
->t('Description'),
$this
->t('Group'),
$this
->t('Weight'),
$this
->t('Operations'),
),
'#empty' => $this
->t('There are no contexts defined.'),
'#attributes' => array(
'id' => 'contexts',
),
);
$group_options = [];
// @todo Make this a bit prettier.
foreach ($groups as $group => $contexts) {
$group_options[$group] = $group === 'not_grouped' ? $this
->t('Not grouped') : $group;
}
// Count the number of entities to get a good delta for the weights.
$weight_delta = round(count($this
->getEntityIds()) / 2);
foreach ($groups as $group => $contexts) {
$group_class = Html::getClass($group);
$form['contexts']['#tabledrag'][] = array(
'action' => 'match',
'relationship' => 'sibling',
'group' => 'context-group-select',
'subgroup' => 'context-group-' . $group_class,
'hidden' => FALSE,
);
$form['contexts']['#tabledrag'][] = array(
'action' => 'order',
'relationship' => 'sibling',
'group' => 'context-weight',
'subgroup' => 'context-weight-' . $group_class,
);
$form['contexts']['group-' . $group_class] = array(
'#attributes' => array(
'class' => array(
'group-label',
'group-label-' . $group_class,
),
'no_striping' => TRUE,
),
);
$form['contexts']['group-' . $group_class] = [
'#attributes' => [
'class' => [
'region-title',
],
],
'title' => [
'#markup' => $group === 'not_grouped' ? $this
->t('Not grouped') : $group,
'#wrapper_attributes' => [
'colspan' => 5,
],
],
];
/** @var ContextInterface $context */
foreach ($contexts as $context_id => $context) {
$operations = [
'edit' => [
'title' => $this
->t('Edit'),
'url' => $context
->urlInfo('edit-form'),
],
'delete' => [
'title' => $this
->t('Delete'),
'url' => $context
->urlInfo('delete-form'),
'attributes' => $this
->getAjaxAttributes(),
],
'disable' => [
'title' => $context
->disabled() ? $this
->t('Enable') : $this
->t('Disable'),
'url' => $context
->toUrl('disable-form'),
'attributes' => $this
->getAjaxAttributes(),
],
];
$form['contexts'][$context_id] = [
'#attributes' => [
'class' => [
'draggable',
],
],
'label' => [
'#markup' => $context
->getLabel(),
'#wrapper_attributes' => $context
->disabled() ? [
'style' => 'opacity:0.6',
] : NULL,
],
'description' => [
'#markup' => $context
->getDescription(),
],
'group' => [
'#type' => 'select',
'#title' => $this
->t('Group for @context context', [
'@context' => $context
->getLabel(),
]),
'#title_display' => 'invisible',
'#default_value' => $context
->getGroup(),
'#options' => $group_options,
'#attributes' => [
'class' => [
'context-group-select',
'context-group-' . $group_class,
],
],
],
'weight' => [
'#type' => 'weight',
'#title' => $this
->t('Weight for @context context', [
'@context' => $context
->getLabel(),
]),
'#default_value' => $context
->getWeight(),
'#delta' => $weight_delta,
'#title_display' => 'invisible',
'#attributes' => [
'class' => [
'context-weight',
'context-weight-' . $group_class,
],
],
],
'operations' => [
'#type' => 'operations',
'#links' => $operations,
],
];
}
}
$form['actions'] = array(
'#type' => 'actions',
);
if (count($groups) > 0) {
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this
->t('Save contexts'),
'#button_type' => 'primary',
);
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// No validation.
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$contexts = $this->storage
->loadMultiple(array_keys($form_state
->getValue('contexts')));
/*** @var ContextInterface $context */
foreach ($contexts as $context_id => $context) {
$context_values = $form_state
->getValue([
'contexts',
$context_id,
]);
$context
->setWeight($context_values['weight']);
// Not grouped contexts needs a specific group value.
if ($context_values['group'] === 'not_grouped') {
$context
->setGroup(Context::CONTEXT_GROUP_NONE);
}
else {
$context
->setGroup($context_values['group']);
}
$context
->save();
}
drupal_set_message($this
->t('The context settings have been updated.'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxFormTrait:: |
public static | function | Gets attributes for use with an AJAX modal. | |
AjaxFormTrait:: |
public static | function | Gets attributes for use with an add button AJAX modal. | |
ConfigEntityListBuilder:: |
public | function |
Gets this list's default operations. Overrides EntityListBuilder:: |
15 |
ConfigEntityListBuilder:: |
public | function |
Loads entities of this type from storage for listing. Overrides EntityListBuilder:: |
7 |
ContextListBuilder:: |
protected | property | The Context modules context manager. | |
ContextListBuilder:: |
protected | property | ||
ContextListBuilder:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ContextListBuilder:: |
public static | function |
Instantiates a new instance of this entity handler. Overrides EntityListBuilder:: |
|
ContextListBuilder:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ContextListBuilder:: |
public | function |
Use a form instead of the entity list builder to display contexts. Overrides EntityListBuilder:: |
|
ContextListBuilder:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ContextListBuilder:: |
public | function |
Form validation handler. Overrides FormInterface:: |
|
ContextListBuilder:: |
public | function |
Constructs a new ContextListBuilder object. Overrides EntityListBuilder:: |
|
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 | |
EntityHandlerBase:: |
protected | property | The module handler to invoke hooks on. | 2 |
EntityHandlerBase:: |
protected | function | Gets the module handler. | 2 |
EntityHandlerBase:: |
public | function | Sets the module handler for this handler. | |
EntityListBuilder:: |
protected | property | Information about the entity type. | |
EntityListBuilder:: |
protected | property | The entity type ID. | |
EntityListBuilder:: |
protected | property | The number of entities to list per page, or FALSE to list all entities. | 3 |
EntityListBuilder:: |
protected | property | The entity storage class. | 1 |
EntityListBuilder:: |
public | function | Builds the header row for the entity listing. | 26 |
EntityListBuilder:: |
public | function | Builds a renderable list of operation links for the entity. | 2 |
EntityListBuilder:: |
public | function | Builds a row for an entity in the entity listing. | 26 |
EntityListBuilder:: |
protected | function | Ensures that a destination is present on the given URL. | |
EntityListBuilder:: |
protected | function | Loads entity IDs using a pager sorted by the entity id. | 4 |
EntityListBuilder:: |
protected | function | Gets the label of an entity. | |
EntityListBuilder:: |
public | function |
Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface:: |
2 |
EntityListBuilder:: |
public | function |
Gets the entity storage. Overrides EntityListBuilderInterface:: |
|
EntityListBuilder:: |
protected | function | Gets the title of the page. | 1 |
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. |