You are here

public function JsonEditorWidget::settingsForm in JSON Field 8

Returns a form to configure settings for the widget.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the widget. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

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

Return value

array The form definition for the widget settings.

Overrides WidgetBase::settingsForm

File

src/Plugin/Field/FieldWidget/JsonEditorWidget.php, line 43

Class

JsonEditorWidget
Plugin implementation of the 'json_editor' widget.

Namespace

Drupal\json_field\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $elements = [];
  $modes = [
    'text' => t('Plain text'),
    'code' => t('Code Editor (ACE)'),
    'tree' => t('Tree'),
    'form' => t('Form (read-only structure)'),
    'view' => t('View (read-only)'),
  ];
  $elements['mode'] = [
    '#type' => 'select',
    '#options' => $modes,
    '#title' => t('Editor mode'),
    '#default_value' => $this
      ->getSetting('mode'),
  ];
  $elements['modes'] = [
    '#type' => 'checkboxes',
    '#options' => $modes,
    '#title' => t('Available modes'),
    '#default_value' => $this
      ->getEditorModes(),
  ];
  $elements['schema'] = [
    '#type' => 'textarea',
    '#title' => t('JSON schema to validate the field'),
    '#default_value' => $this
      ->getSetting('schema'),
    '#attributes' => [
      'data-json-editor' => 'admin',
    ],
    '#attached' => [
      'library' => [
        'json_field/json_editor.widget',
      ],
      'drupalSettings' => [
        'json_field' => [
          'admin' => [
            'mode' => 'code',
            'modes' => [
              'tree',
              'code',
              'text',
            ],
            'schema' => file_get_contents(__DIR__ . '/../../../../assets/schema.json'),
          ],
        ],
      ],
    ],
    '#element_validate' => [
      [
        get_class($this),
        'validateJsonSchema',
      ],
    ],
  ];
  $elements['schema_validate'] = [
    '#type' => 'checkbox',
    '#title' => t('Validate against the schema'),
    '#description' => t('Uses theJSON schema provided above to validate the data entered, prevents saving the entity if the JSON is not valid against the schema.'),
    '#default_value' => $this
      ->getSetting('schema_validate'),
  ];
  return $elements;
}