DomainSelection.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Drupal\domain\Plugin\EntityReferenceSelection;
  3. use Drupal\user\Entity\User;
  4. use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
  5. use Drupal\Core\Form\FormStateInterface;
  6. /**
  7. * Provides entity reference selections for the domain entity type.
  8. *
  9. * @EntityReferenceSelection(
  10. * id = "default:domain",
  11. * label = @Translation("Domain selection"),
  12. * entity_types = {"domain"},
  13. * group = "default",
  14. * weight = 1
  15. * )
  16. */
  17. class DomainSelection extends DefaultSelection {
  18. /**
  19. * Sets the context for the alter hook.
  20. *
  21. * @var string
  22. */
  23. protected $fieldType = 'editor';
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
  28. $query = parent::buildEntityQuery($match, $match_operator);
  29. // Let administrators do anything.
  30. if ($this->currentUser->hasPermission('administer domains')) {
  31. return $query;
  32. }
  33. // Can this user access inactive domains?
  34. if (!$this->currentUser->hasPermission('access inactive domains')) {
  35. $query->condition('status', 1);
  36. }
  37. // Filter domains by the user's assignments, which are controlled by other
  38. // modules. Those modules must know what type of entity they are dealing
  39. // with, so look up the entity type and bundle.
  40. $info = $query->getMetaData('entity_reference_selection_handler');
  41. if (!empty($info->configuration['entity'])) {
  42. $context['entity_type'] = $info->configuration['entity']->getEntityTypeId();
  43. $context['bundle'] = $info->configuration['entity']->bundle();
  44. $context['field_type'] = $this->fieldType;
  45. // Load the current user.
  46. $account = User::load($this->currentUser->id());
  47. // Run the alter hook.
  48. $this->moduleHandler->alter('domain_references', $query, $account, $context);
  49. }
  50. return $query;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  56. $selection_handler_settings = $this->configuration['handler_settings'];
  57. // Merge-in default values.
  58. $selection_handler_settings += [
  59. // For the 'target_bundles' setting, a NULL value is equivalent to "allow
  60. // entities from any bundle to be referenced" and an empty array value is
  61. // equivalent to "no entities from any bundle can be referenced".
  62. 'target_bundles' => NULL,
  63. 'sort' => [
  64. 'field' => 'weight',
  65. 'direction' => 'ASC',
  66. ],
  67. 'auto_create' => FALSE,
  68. 'default_selection' => 'current',
  69. ];
  70. $form['target_bundles'] = [
  71. '#type' => 'value',
  72. '#value' => NULL,
  73. ];
  74. $fields = [
  75. 'weight' => $this->t('Weight'),
  76. 'label' => $this->t('Name'),
  77. 'hostname' => $this->t('Hostname'),
  78. ];
  79. $form['sort']['field'] = [
  80. '#type' => 'select',
  81. '#title' => $this->t('Sort by'),
  82. '#options' => $fields,
  83. '#ajax' => FALSE,
  84. '#default_value' => $selection_handler_settings['sort']['field'],
  85. ];
  86. $form['sort']['direction'] = [
  87. '#type' => 'select',
  88. '#title' => $this->t('Sort direction'),
  89. '#required' => TRUE,
  90. '#options' => [
  91. 'ASC' => $this->t('Ascending'),
  92. 'DESC' => $this->t('Descending'),
  93. ],
  94. '#default_value' => $selection_handler_settings['sort']['direction'],
  95. ];
  96. return $form;
  97. }
  98. }