class ParagraphSelection in Paragraphs 8
Default plugin implementation of the Entity Reference Selection plugin.
Plugin annotation
@EntityReferenceSelection(
id = "default:paragraph",
label = @Translation("Paragraphs"),
group = "default",
entity_types = {"paragraph"},
weight = 0
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase implements ConfigurableInterface, ConfigurablePluginInterface, DependentPluginInterface, SelectionInterface
- class \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection implements SelectionWithAutocreateInterface, ContainerFactoryPluginInterface uses DeprecatedServicePropertyTrait
- class \Drupal\paragraphs\Plugin\EntityReferenceSelection\ParagraphSelection
- class \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection implements SelectionWithAutocreateInterface, ContainerFactoryPluginInterface uses DeprecatedServicePropertyTrait
- class \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase implements ConfigurableInterface, ConfigurablePluginInterface, DependentPluginInterface, SelectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ParagraphSelection
3 files declare their use of ParagraphSelection
- InlineParagraphsWidget.php in src/
Plugin/ Field/ FieldWidget/ InlineParagraphsWidget.php - ParagraphsLibraryItemHasAllowedParagraphsTypeConstraintValidator.php in modules/
paragraphs_library/ src/ Plugin/ Validation/ Constraint/ ParagraphsLibraryItemHasAllowedParagraphsTypeConstraintValidator.php - ParagraphsWidget.php in src/
Plugin/ Field/ FieldWidget/ ParagraphsWidget.php
File
- src/
Plugin/ EntityReferenceSelection/ ParagraphSelection.php, line 21
Namespace
Drupal\paragraphs\Plugin\EntityReferenceSelectionView source
class ParagraphSelection extends DefaultSelection {
/**
* @inheritDoc
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'negate' => 0,
'target_bundles_drag_drop' => [],
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$entity_type_id = $this->configuration['target_type'];
$bundles = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
$bundle_options = array();
$bundle_options_simple = array();
// Default weight for new items.
$weight = count($bundles) + 1;
foreach ($bundles as $bundle_name => $bundle_info) {
$bundle_options_simple[$bundle_name] = $bundle_info['label'];
$bundle_options[$bundle_name] = array(
'label' => $bundle_info['label'],
'enabled' => $this->configuration['target_bundles_drag_drop'][$bundle_name]['enabled'] ?? FALSE,
'weight' => $this->configuration['target_bundles_drag_drop'][$bundle_name]['weight'] ?? $weight,
);
$weight++;
}
// Do negate the selection.
$form['negate'] = [
'#type' => 'radios',
'#options' => [
1 => $this
->t('Exclude the selected below'),
0 => $this
->t('Include the selected below'),
],
'#title' => $this
->t('Which Paragraph types should be allowed?'),
'#default_value' => $this->configuration['negate'],
];
// Kept for compatibility with other entity reference widgets.
$form['target_bundles'] = array(
'#type' => 'checkboxes',
'#options' => $bundle_options_simple,
'#default_value' => $this->configuration['target_bundles'] ?? [],
'#access' => FALSE,
);
if ($bundle_options) {
$form['target_bundles_drag_drop'] = [
'#element_validate' => [
[
__CLASS__,
'targetTypeValidate',
],
],
'#type' => 'table',
'#header' => [
$this
->t('Type'),
$this
->t('Weight'),
],
'#attributes' => [
'id' => 'bundles',
],
'#prefix' => '<h5>' . $this
->t('Paragraph types') . '</h5>',
'#suffix' => '<div class="description">' . $this
->t('Selection of Paragraph types for this field. Select none to allow all Paragraph types.') . '</div>',
];
$form['target_bundles_drag_drop']['#tabledrag'][] = [
'action' => 'order',
'relationship' => 'sibling',
'group' => 'bundle-weight',
];
}
uasort($bundle_options, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
$weight_delta = $weight;
// Default weight for new items.
$weight = count($bundles) + 1;
foreach ($bundle_options as $bundle_name => $bundle_info) {
$form['target_bundles_drag_drop'][$bundle_name] = array(
'#attributes' => array(
'class' => array(
'draggable',
),
),
);
$form['target_bundles_drag_drop'][$bundle_name]['enabled'] = array(
'#type' => 'checkbox',
'#title' => $bundle_info['label'],
'#title_display' => 'after',
'#default_value' => $bundle_info['enabled'],
);
$form['target_bundles_drag_drop'][$bundle_name]['weight'] = array(
'#type' => 'weight',
'#default_value' => (int) $bundle_info['weight'],
'#delta' => $weight_delta,
'#title' => $this
->t('Weight for type @type', array(
'@type' => $bundle_info['label'],
)),
'#title_display' => 'invisible',
'#attributes' => array(
'class' => array(
'bundle-weight',
'bundle-weight-' . $bundle_name,
),
),
);
$weight++;
}
if (empty($bundle_options)) {
$form['allowed_bundles_explain'] = [
'#type' => 'markup',
'#markup' => $this
->t('You did not add any Paragraph types yet, click <a href=":here">here</a> to add one.', [
':here' => Url::fromRoute('paragraphs.type_add')
->toString(),
]),
];
}
return $form;
}
/**
* Validate helper to have support for other entity reference widgets.
*
* @param $element
* @param FormStateInterface $form_state
* @param $form
*/
public static function targetTypeValidate($element, FormStateInterface $form_state, $form) {
$values =& $form_state
->getValues();
$element_values = NestedArray::getValue($values, $element['#parents']);
$bundle_options = array();
if ($element_values) {
$enabled = 0;
foreach ($element_values as $machine_name => $bundle_info) {
if (isset($bundle_info['enabled']) && $bundle_info['enabled']) {
$bundle_options[$machine_name] = $machine_name;
$enabled++;
}
}
// All disabled = all enabled.
if ($enabled === 0) {
$bundle_options = NULL;
}
}
// New value parents.
$parents = array_merge(array_slice($element['#parents'], 0, -1), array(
'target_bundles',
));
NestedArray::setValue($values, $parents, $bundle_options);
}
/**
* Returns the sorted allowed types for the field.
*
* @return array
* A list of arrays keyed by the paragraph type machine name
* with the following properties.
* - label: The label of the paragraph type.
* - weight: The weight of the paragraph type.
*/
public function getSortedAllowedTypes() {
$return_bundles = [];
$bundles = $this->entityTypeBundleInfo
->getBundleInfo('paragraph');
if (!empty($this->configuration['target_bundles'])) {
if (isset($this->configuration['negate']) && $this->configuration['negate'] == '1') {
$bundles = array_diff_key($bundles, $this->configuration['target_bundles']);
}
else {
$bundles = array_intersect_key($bundles, $this->configuration['target_bundles']);
}
}
// Support for the paragraphs reference type.
if (!empty($this->configuration['target_bundles_drag_drop'])) {
$drag_drop_settings = $this->configuration['target_bundles_drag_drop'];
$max_weight = count($bundles);
foreach ($drag_drop_settings as $bundle_info) {
if (isset($bundle_info['weight']) && $bundle_info['weight'] && $bundle_info['weight'] > $max_weight) {
$max_weight = $bundle_info['weight'];
}
}
// Default weight for new items.
$weight = $max_weight + 1;
foreach ($bundles as $machine_name => $bundle) {
$return_bundles[$machine_name] = [
'label' => $bundle['label'],
'weight' => isset($drag_drop_settings[$machine_name]['weight']) ? $drag_drop_settings[$machine_name]['weight'] : $weight,
];
$weight++;
}
}
else {
$weight = 0;
foreach ($bundles as $machine_name => $bundle) {
$return_bundles[$machine_name] = [
'label' => $bundle['label'],
'weight' => $weight,
];
$weight++;
}
}
uasort($return_bundles, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
return $return_bundles;
}
/**
* {@inheritdoc}
*/
public function validateReferenceableNewEntities(array $entities) {
$bundles = array_keys($this
->getSortedAllowedTypes());
return array_filter($entities, function ($entity) {
if (isset($bundles)) {
return in_array($entity
->bundle(), $bundles);
}
return TRUE;
});
}
/**
* {@inheritdoc}
*/
protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$target_type = $this->configuration['target_type'];
$entity_type = $this->entityTypeManager
->getDefinition($target_type);
$query = $this->entityTypeManager
->getStorage($target_type)
->getQuery();
// If 'target_bundles' is NULL, all bundles are referenceable, no further
// conditions are needed.
if (is_array($this->configuration['target_bundles'])) {
$target_bundles = array_keys($this
->getSortedAllowedTypes());
// If 'target_bundles' is an empty array, no bundle is referenceable,
// force the query to never return anything and bail out early.
if ($target_bundles === []) {
$query
->condition($entity_type
->getKey('id'), NULL, '=');
return $query;
}
else {
$query
->condition($entity_type
->getKey('bundle'), $target_bundles, 'IN');
}
}
if (isset($match) && ($label_key = $entity_type
->getKey('label'))) {
$query
->condition($label_key, $match, $match_operator);
}
// Add entity-access tag.
$query
->addTag($target_type . '_access');
// Add the Selection handler for system_query_entity_reference_alter().
$query
->addTag('entity_reference');
$query
->addMetaData('entity_reference_selection_handler', $this);
// Add the sort option.
if (!empty($this->configuration['sort'])) {
$sort_settings = $this->configuration['sort'];
if ($sort_settings['field'] != '_none') {
$query
->sort($sort_settings['field'], $sort_settings['direction']);
}
}
return $query;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DefaultSelection:: |
protected | property | The current user. | |
DefaultSelection:: |
protected | property | ||
DefaultSelection:: |
protected | property | The entity field manager service. | |
DefaultSelection:: |
protected | property | The entity repository. | |
DefaultSelection:: |
public | property | Entity type bundle info service. | |
DefaultSelection:: |
protected | property | The entity type manager service. | |
DefaultSelection:: |
protected | property | The module handler service. | |
DefaultSelection:: |
public | function |
Counts entities that are referenceable. Overrides SelectionInterface:: |
2 |
DefaultSelection:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
2 |
DefaultSelection:: |
public | function |
Creates a new entity object that can be used as a valid reference. Overrides SelectionWithAutocreateInterface:: |
6 |
DefaultSelection:: |
public static | function | Form element validation handler; Filters the #value property of an element. | |
DefaultSelection:: |
public | function |
Gets the list of referenceable entities. Overrides SelectionInterface:: |
3 |
DefaultSelection:: |
protected | function | Helper method: Passes a query to the alteration system again. | |
DefaultSelection:: |
public | function |
Form validation handler. Overrides SelectionPluginBase:: |
|
DefaultSelection:: |
public | function |
Validates which existing entities can be referenced. Overrides SelectionInterface:: |
|
DefaultSelection:: |
public | function |
Constructs a new DefaultSelection object. Overrides SelectionPluginBase:: |
1 |
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 | |
DeprecatedServicePropertyTrait:: |
public | function | Allows to access deprecated/removed properties. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
ParagraphSelection:: |
public | function |
Form constructor. Overrides DefaultSelection:: |
|
ParagraphSelection:: |
protected | function |
Builds an EntityQuery to get referenceable entities. Overrides DefaultSelection:: |
|
ParagraphSelection:: |
public | function |
@inheritDoc Overrides DefaultSelection:: |
|
ParagraphSelection:: |
public | function | Returns the sorted allowed types for the field. | |
ParagraphSelection:: |
public static | function | Validate helper to have support for other entity reference widgets. | |
ParagraphSelection:: |
public | function |
Validates which newly created entities can be referenced. Overrides DefaultSelection:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
SelectionPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
SelectionPluginBase:: |
protected | function | Ensures a backward compatibility level configuration. | |
SelectionPluginBase:: |
public | function |
Allows the selection to alter the SelectQuery generated by EntityFieldQuery. Overrides SelectionInterface:: |
2 |
SelectionPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
SelectionPluginBase:: |
protected | function | Moves the backward compatibility level configurations in the right place. | |
SelectionPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
SelectionPluginBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
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. |