View source
<?php
namespace Drupal\toggle_editable_fields\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\FieldConfigInterface;
class AjaxToggleForm extends FormBase {
protected $fieldItem;
protected $fieldDefinition;
protected $entity;
protected $fieldName;
protected $delta;
protected $defaultValue;
protected $fieldSettings;
public function setFieldItem(FieldItemInterface $item, array $settings = []) {
$this->fieldItem = $item;
$this->fieldDefinition = $item
->getFieldDefinition();
$this->entity = $this->fieldItem
->getEntity();
$this->fieldName = $this->fieldDefinition
->getName();
$this->delta = $this->fieldItem
->getName();
$this->defaultValue = $this->fieldItem->value;
$this->fieldSettings = $settings;
}
public function getFormId() {
$parts = [
'editable_ajax_toggle',
$this->entity
->getEntityTypeId(),
$this->entity
->id(),
$this->fieldName,
$this->delta,
'form',
];
return implode('_', $parts);
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#tree'] = TRUE;
$form['checkbox'] = [
'#type' => 'checkbox',
'#default_value' => $this->defaultValue,
'#attributes' => [
'data-toggle' => 'toggle',
'class' => [
'checkbox-toggle',
],
],
'#ajax' => [
'callback' => [
$this,
'formListAjax',
],
'event' => 'change',
'progress' => [
'type' => 'none',
],
],
'#disabled' => !($this
->fieldIsEditable() || $this
->checkEditFieldAccess()),
];
$form['#attached']['library'][] = 'toggle_editable_fields/bootstrap.toogle';
$this
->setBooststrapDataAttributes($form['checkbox']);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public function formListAjax(array &$form, FormStateInterface $form_state) {
$element = $form_state
->getTriggeringElement();
if (!empty($element)) {
$this
->updateFieldValue($form_state
->getValue($element['#parents']));
}
return new AjaxResponse();
}
public function updateFieldValue($value) {
if (!$this->entity
->hasField($this->fieldName)) {
throw new \Exception("No field {$this->fieldName} found in {$this->entity->id()} entity.");
}
if ($this
->fieldIsEditable() || $this
->checkEditFieldAccess()) {
$this->entity
->get($this->fieldName)
->set($this->delta, $value);
$this->entity
->save();
}
}
public function setBooststrapDataAttributes(array &$element) {
foreach ($this->fieldSettings as $data_id => $data_value) {
if ($data_value != NULL && !isset($element['#attributes']["data-{$data_id}"])) {
$element['#attributes']["data-{$data_id}"] = $data_value;
}
}
}
public function fieldIsEditable() {
return $this->entity
->access('update') && $this
->checkEditFieldAccess();
}
public function checkEditFieldAccess() {
$permission_type = $this->fieldDefinition instanceof FieldConfigInterface ? $this->fieldDefinition
->getFieldStorageDefinition()
->getThirdPartySetting('field_permissions', 'permission_type') : NULL;
$field_access = \Drupal::entityTypeManager()
->getAccessControlHandler($this->entity
->getEntityTypeId())
->fieldAccess('edit', $this->fieldDefinition, \Drupal::currentUser(), $this->fieldItem
->getParent());
if ($field_access && empty($permission_type)) {
return $this->entity
->access('update');
}
return $field_access;
}
}