View source
<?php
namespace Drupal\entityconnect;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\views\Views;
class EntityconnectWidgetProcessor {
use StringTranslationTrait;
protected $fieldDefinition;
protected $widget;
protected $entityconnectSettings;
protected $entityType;
protected $acceptableTypes;
public function __construct(FieldConfig $field_definition, array $widget) {
$this->fieldDefinition = $field_definition;
$this->widget = $widget;
$this->entityconnectSettings = $this->fieldDefinition
->getThirdPartySettings('entityconnect');
if (!$this->entityconnectSettings) {
$this->entityconnectSettings = \Drupal::config('entityconnect.administration_config')
->get();
}
$this
->initTargetInfo();
}
public static function process(array $element, FormStateInterface $form_state, array $form) {
if (!method_exists($form_state
->getFormObject(), 'getEntity')) {
return $element;
}
$entity = $form_state
->getFormObject()
->getEntity();
$fieldDefinition = $entity
->getFieldDefinition($element['widget']['#field_name']);
$widgetProcessor = new EntityconnectWidgetProcessor($fieldDefinition, $element['widget']);
$entityType = $widgetProcessor
->getEntityType();
$acceptableTypes = $widgetProcessor
->getAcceptableTypes();
$data = [
'entity_type' => &$entityType,
'acceptable_types' => &$acceptableTypes,
'field' => $fieldDefinition,
];
\Drupal::moduleHandler()
->alter('entityconnect_field_attach_form', $data);
$widgetProcessor
->setEntityType($data['entity_type']);
$widgetProcessor
->setAcceptableTypes($data['acceptable_types']);
if (isset($element['widget']['#type']) || isset($element['widget']['target_id'])) {
$widgetProcessor
->attachButtons($element);
}
else {
foreach (Element::getVisibleChildren($element['widget']) as $key) {
if (is_numeric($key)) {
$widgetProcessor
->attachButtons($element, $key);
}
}
}
return $element;
}
protected function attachButtons(array &$element, $key = 'all') {
$parents = '';
if (isset($this->widget['#field_parents'])) {
foreach ($this->widget['#field_parents'] as $parent) {
$parents .= ($parents ? '-' : '') . $parent;
}
}
$fieldStorage = $this->fieldDefinition
->getFieldStorageDefinition();
$extraClass = isset($this->widget['#type']) ? $this->widget['#type'] : 'autocomplete';
$extraClass .= $fieldStorage
->getCardinality() > 1 ? ' multiple-values' : ' single-value';
$extraClass .= isset($this->widget['#multiple']) && $this->widget['#multiple'] == TRUE ? ' multiple-selection' : ' single-selection';
if (isset($this->widget['#type'])) {
if (isset($this->widget['#multiple']) && $this->widget['#multiple'] == TRUE || $this->widget['#type'] == 'radios' || $this->widget['#type'] == 'checkboxes') {
$element['#attributes']['class'][] = 'inline-label';
}
}
$buttonClasses = [
'extra_class' => $extraClass,
'parents_class' => $parents,
];
if ($key === 'all') {
if (isset($this->widget['#type'])) {
$widgetElement =& $element;
}
else {
$widgetElement =& $element['widget'];
}
}
else {
$widgetElement =& $element['widget'][$key];
}
$this
->attachAddButton($widgetElement, $buttonClasses, $key);
$this
->attachEditButton($widgetElement, $buttonClasses, $key);
}
protected function attachAddButton(array &$element, array $entityconnect_classes, $key = 'all') {
$addbuttonallowed = empty($this->entityconnectSettings['buttons']['button_add']);
$addIcon = $this->entityconnectSettings['icons']['icon_add'];
$acceptableTypes = [];
if (!$this->acceptableTypes) {
if (\Drupal::entityTypeManager()
->getAccessControlHandler($this->entityType)
->createAccess($this->entityType)) {
$acceptableTypes[] = $this->entityType;
}
}
else {
foreach ($this->acceptableTypes as $bundle) {
if (\Drupal::entityTypeManager()
->getAccessControlHandler($this->entityType)
->createAccess($bundle)) {
$acceptableTypes[] = $bundle;
}
}
}
if (\Drupal::currentUser()
->hasPermission('entityconnect add button') && $addbuttonallowed && $acceptableTypes) {
$classes = [];
if (isset($addIcon)) {
if ($addIcon == '0') {
$classes = $entityconnect_classes['extra_class'] . ' add-icon';
}
elseif ($addIcon == '1') {
$classes = $entityconnect_classes['extra_class'] . ' add-icon add-text';
}
else {
$classes = $entityconnect_classes['extra_class'];
}
}
$button_name = "add_entityconnect__{$this->fieldDefinition->getName()}_{$key}_{$entityconnect_classes['parents_class']}";
$element[$button_name] = [
'#type' => 'entityconnect_submit',
'#value' => $this
->t('New content'),
'#name' => $button_name,
'#prefix' => "<div class = 'entityconnect-add {$classes}'>",
'#suffix' => '</div>',
'#key' => $key,
'#field' => $this->fieldDefinition
->getName(),
'#entity_type_target' => $this->entityType,
'#acceptable_types' => $acceptableTypes,
'#add_child' => TRUE,
'#weight' => 1,
];
$parents = $this->widget['#parents'];
if (is_numeric($key)) {
$parents[] = $key;
}
$element[$button_name]['#parents'] = array_merge($parents, [
$button_name,
]);
}
}
protected function attachEditButton(array &$element, array $entityconnect_classes, $key = 'all') {
$editbuttonallowed = empty($this->entityconnectSettings['buttons']['button_edit']);
$editIcon = $this->entityconnectSettings['icons']['icon_edit'];
if (\Drupal::currentUser()
->hasPermission('entityconnect edit button') && $editbuttonallowed) {
$classes = [];
if (isset($editIcon)) {
if ($editIcon == '0') {
$classes = $entityconnect_classes['extra_class'] . ' edit-icon';
}
elseif ($editIcon == '1') {
$classes = $entityconnect_classes['extra_class'] . ' edit-icon edit-text';
}
else {
$classes = $entityconnect_classes['extra_class'];
}
}
$button_name = "edit_entityconnect__{$this->fieldDefinition->getName()}_{$key}_{$entityconnect_classes['parents_class']}";
$element[$button_name] = [
'#type' => 'entityconnect_submit',
'#value' => $this
->t('Edit content'),
'#name' => $button_name,
'#prefix' => "<div class = 'entityconnect-edit {$classes}'>",
'#suffix' => '</div>',
'#key' => $key,
'#field' => $this->fieldDefinition
->getName(),
'#entity_type_target' => $this->entityType,
'#acceptable_types' => $this->acceptableTypes,
'#add_child' => FALSE,
'#weight' => 1,
];
$parents = $this->widget['#parents'];
if (is_numeric($key)) {
$parents[] = $key;
}
$element[$button_name]['#parents'] = array_merge($parents, [
$button_name,
]);
}
}
public function getAcceptableTypes() {
return $this->acceptableTypes;
}
public function getEntityType() {
return $this->entityType;
}
public function setEntityType($entityType) {
$this->entityType = $entityType;
}
public function setAcceptableTypes(array $acceptableTypes) {
$this->acceptableTypes = $acceptableTypes;
}
protected function initTargetInfo() {
$targetSettings = $this->fieldDefinition
->getSettings();
$this->entityType = $targetSettings['target_type'];
$this->acceptableTypes = [];
if (isset($targetSettings['handler_settings']['target_bundles'])) {
if (!is_null($targetSettings['handler_settings']['target_bundles'])) {
$this->acceptableTypes = $targetSettings['handler_settings']['target_bundles'];
}
else {
$this->acceptableTypes[] = $this->entityType;
}
}
elseif ($targetSettings['handler'] == 'views') {
$view = Views::getView($targetSettings['handler_settings']['view']['view_name']);
$viewDisplay = $view->storage
->getDisplay($targetSettings['handler_settings']['view']['display_name']);
if (!isset($viewDisplay['display_options']['filters'])) {
$viewDisplay = $view->storage
->getDisplay('default');
}
switch ($this->entityType) {
case 'taxonomy_term':
if (isset($viewDisplay['display_options']['filters']['vid'])) {
$this->acceptableTypes = $viewDisplay['display_options']['filters']['vid']['value'];
}
break;
default:
if (isset($viewDisplay['display_options']['filters']['type'])) {
$this->acceptableTypes = $viewDisplay['display_options']['filters']['type']['value'];
}
break;
}
}
}
}