You are here

public function EditorNoteWidget::onNoteSubmit in Editor Notes 8

Ajax callback for 'Add Note button'.

Parameters

array $form: The form structure where widgets are being attached to. This might be a full form structure, or a sub-element of a larger form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array Form element.

File

src/Plugin/Field/FieldWidget/EditorNoteWidget.php, line 253

Class

EditorNoteWidget
Editor note widget.

Namespace

Drupal\editor_note\Plugin\Field\FieldWidget

Code

public function onNoteSubmit(array $form, FormStateInterface $form_state) {
  $button = $form_state
    ->getTriggeringElement();
  $element =& NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
  $host_entity = $form_state
    ->getFormObject()
    ->getEntity();
  $field_name = $button['#array_parents'][0];

  // @todo: implement support for text formats.
  $format = 'plain_text';

  // Get value depending  on formatted field or not.
  if ($this
    ->getSetting('formatted')) {
    $value = $element['editor_notes']['value']['#value'];
    $format = $element['editor_notes']['value']['#format'];
  }
  else {
    $value = $element['editor_notes']['#value'];
  }
  if (!empty(trim($value))) {

    // Save Editor Note entity.
    $this->editorNoteHelper
      ->createNote($host_entity, $field_name, $value, $format);
  }
  $notes = $this->editorNoteHelper
    ->getNotesByEntityAndField($host_entity
    ->id(), $field_name, $this
    ->getSettings());

  // Rebuild Editor Notes table.
  $table = $this->editorNoteHelper
    ->generateTable($this->fieldDefinition, $notes, TRUE);

  // Update some element values.
  $element['table'] = $table;
  $element['#open'] = TRUE;
  if ($this
    ->getSetting('formatted')) {
    $element['editor_notes']['value']['#value'] = '';
  }
  else {
    $element['editor_notes']['#value'] = '';
  }
  return $element;
}