DomainElementManager.php in Domain Access 8
File
domain/src/DomainElementManager.php
View source
<?php
namespace Drupal\domain;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class DomainElementManager implements DomainElementManagerInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $domainStorage;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->domainStorage = $entity_type_manager
->getStorage('domain');
}
public function setFormOptions(array $form, FormStateInterface $form_state, $field_name, $hide_on_disallow = FALSE) {
if (!isset($form[$field_name])) {
return $form;
}
$fields = $this
->fieldList($field_name);
$empty = FALSE;
$disallowed = $this
->disallowedOptions($form_state, $form[$field_name]);
if (empty($form[$field_name]['widget']['#options']) || count($form[$field_name]['widget']['#options']) == 1 && isset($form[$field_name]['widget']['#options']['_none'])) {
$empty = TRUE;
}
if (isset($form['domain']) && !isset($form[$field_name]['#group'])) {
$form[$field_name]['#group'] = 'domain';
}
if ($hide_on_disallow && $empty) {
$form[$field_name]['#access'] = FALSE;
}
if (!empty($disallowed) || $empty) {
$form[$field_name . '_disallowed'] = [
'#type' => 'value',
'#value' => $disallowed,
];
$form['domain_hidden_fields'] = [
'#type' => 'value',
'#value' => $fields,
];
if ($hide_on_disallow || $empty) {
$form[$field_name]['#access'] = FALSE;
}
elseif (!empty($disallowed)) {
$form[$field_name]['widget']['#description'] .= $this
->listDisallowed($disallowed);
}
$buttons = [
'preview',
'delete',
];
$submit = $this
->getSubmitHandler();
foreach ($form['actions'] as $key => $action) {
if (!in_array($key, $buttons) && isset($form['actions'][$key]['#submit']) && !in_array($submit, $form['actions'][$key]['#submit'])) {
array_unshift($form['actions'][$key]['#submit'], $submit);
}
}
}
return $form;
}
public static function submitEntityForm(array &$form, FormStateInterface $form_state) {
$fields = $form_state
->getValue('domain_hidden_fields');
foreach ($fields as $field) {
$entity_values = [];
$values = $form_state
->getValue($field . '_disallowed');
if (!empty($values)) {
$info = $form_state
->getBuildInfo();
$node = $form_state
->getFormObject()
->getEntity();
$entity_values = $form_state
->getValue($field);
}
if (is_array($values)) {
foreach ($values as $value) {
$entity_values[]['target_id'] = $value;
}
}
else {
$entity_values[]['target_id'] = $values;
}
if (!empty($entity_values)) {
$form_state
->setValue($field, $entity_values);
}
}
}
public function disallowedOptions(FormStateInterface $form_state, array $field) {
$options = [];
$info = $form_state
->getBuildInfo();
$entity = $form_state
->getFormObject()
->getEntity();
$entity_values = $this
->getFieldValues($entity, $field['widget']['#field_name']);
if (isset($field['widget']['#options'])) {
$options = array_diff_key($entity_values, $field['widget']['#options']);
}
return array_keys($options);
}
public function fieldList($field_name) {
static $fields = [];
$fields[] = $field_name;
return array_unique($fields);
}
public function getFieldValues(EntityInterface $entity, $field_name) {
$list = [];
if (is_null($entity)) {
return $list;
}
$values = $entity
->hasField($field_name) ? $entity
->get($field_name) : NULL;
if (!empty($values)) {
foreach ($values as $item) {
if ($target = $item
->getValue()) {
if ($domain = $this->domainStorage
->load($target['target_id'])) {
$list[$domain
->id()] = $domain
->getDomainId();
}
}
}
}
return $list;
}
public function getSubmitHandler() {
return '\\Drupal\\domain\\DomainElementManager::submitEntityForm';
}
public function listDisallowed(array $disallowed) {
$domains = $this->domainStorage
->loadMultiple($disallowed);
$string = $this
->t('The following domains are currently assigned and cannot be changed:');
foreach ($domains as $domain) {
$items[] = $domain
->label();
}
$build = [
'#theme' => 'item_list',
'#items' => $items,
];
$string .= render($build);
return '<div class="disallowed">' . $string . '</div>';
}
}