public function ServerForm::form in GraphQL 8.4
Gets the actual form array to be built.
Overrides EntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- src/
Form/ ServerForm.php, line 88
Class
- ServerForm
- Admin form to set up a GraphQL server configuration entity.
Namespace
Drupal\graphql\FormCode
public function form(array $form, FormStateInterface $formState) : array {
$form = parent::form($form, $formState);
/** @var \Drupal\graphql\Entity\ServerInterface $server */
$server = $this->entity;
$schemas = array_map(function ($definition) {
return $definition['name'] ?? $definition['id'];
}, $this->schemaManager
->getDefinitions());
$schema_keys = array_keys($schemas);
$input = $formState
->getUserInput();
// Use the schema selected by the user, the one configured, or fall back to
// the first schema that is defined.
$schema = $input['schema'] ?? $server
->get('schema') ?: reset($schema_keys);
if ($this->operation == 'add') {
$form['#title'] = $this
->t('Add server');
}
else {
$form['#title'] = $this
->t('Edit %label server', [
'%label' => $server
->label(),
]);
}
$form['label'] = [
'#title' => $this
->t('Label'),
'#type' => 'textfield',
'#default_value' => $server
->label(),
'#description' => $this
->t('The human-readable name of this server.'),
'#required' => TRUE,
'#size' => 30,
];
$form['name'] = [
'#type' => 'machine_name',
'#default_value' => $server
->id(),
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'#machine_name' => [
'exists' => [
'Drupal\\graphql\\Entity\\Server',
'load',
],
'source' => [
'label',
],
],
'#description' => $this
->t('A unique machine-readable name for this server. It must only contain lowercase letters, numbers, and underscores.'),
];
$form['schema'] = [
'#title' => $this
->t('Schema'),
'#type' => 'select',
'#options' => $schemas,
'#default_value' => $schema,
'#description' => $this
->t('The schema to use with this server.'),
'#ajax' => [
'callback' => '::ajaxSchemaConfigurationForm',
'progress' => [
'type' => 'throbber',
'message' => $this
->t('Updating schema configuration form.'),
],
],
];
$form['schema_configuration'] = [
'#type' => 'container',
'#prefix' => '<div id="edit-schema-configuration-plugin-wrapper">',
'#suffix' => '</div>',
'#tree' => TRUE,
];
/** @var \Drupal\graphql\Plugin\SchemaPluginInterface $instance */
$instance = $schema ? $this->schemaManager
->createInstance($schema) : NULL;
if ($instance instanceof PluginFormInterface && $instance instanceof ConfigurableInterface) {
$instance
->setConfiguration($server
->get('schema_configuration')[$schema] ?? []);
$form['schema_configuration'][$schema] = [
'#type' => 'fieldset',
'#title' => $this
->t('Schema configuration'),
'#tree' => TRUE,
];
$form['schema_configuration'][$schema] += $instance
->buildConfigurationForm([], $formState);
}
$form['endpoint'] = [
'#title' => $this
->t('Endpoint'),
'#type' => 'textfield',
'#default_value' => $server
->get('endpoint'),
'#description' => $this
->t('The endpoint for http queries. Has to start with a forward slash. For example "/graphql".'),
'#required' => TRUE,
'#size' => 30,
'#field_prefix' => $this->requestContext
->getCompleteBaseUrl(),
];
$form['batching'] = [
'#title' => $this
->t('Allow query batching'),
'#type' => 'checkbox',
'#default_value' => !!$server
->get('batching'),
'#description' => $this
->t('Whether batched queries are allowed.'),
];
$form['caching'] = [
'#title' => $this
->t('Enable caching'),
'#type' => 'checkbox',
'#default_value' => !!$server
->get('caching'),
'#description' => $this
->t('Whether caching of queries and partial results is enabled.'),
];
$debug_flags = $server
->get('debug_flag') ?? 0;
$form['debug_flag'] = [
'#title' => $this
->t('Debug settings'),
'#type' => 'checkboxes',
'#options' => [
DebugFlag::INCLUDE_DEBUG_MESSAGE => $this
->t("Add debugMessage key containing the exception message to errors."),
DebugFlag::INCLUDE_TRACE => $this
->t("Include the formatted original backtrace in errors."),
DebugFlag::RETHROW_INTERNAL_EXCEPTIONS => $this
->t("Rethrow the internal GraphQL exceptions"),
DebugFlag::RETHROW_UNSAFE_EXCEPTIONS => $this
->t("Rethrow unsafe GraphQL exceptions, these are exceptions that have not been marked as safe to expose to clients."),
],
'#default_value' => array_keys(array_filter([
DebugFlag::INCLUDE_DEBUG_MESSAGE => (bool) ($debug_flags & DebugFlag::INCLUDE_DEBUG_MESSAGE),
DebugFlag::INCLUDE_TRACE => (bool) ($debug_flags & DebugFlag::INCLUDE_TRACE),
DebugFlag::RETHROW_INTERNAL_EXCEPTIONS => (bool) ($debug_flags & DebugFlag::RETHROW_INTERNAL_EXCEPTIONS),
DebugFlag::RETHROW_UNSAFE_EXCEPTIONS => (bool) ($debug_flags & DebugFlag::RETHROW_UNSAFE_EXCEPTIONS),
])),
'#description' => $this
->t("It is recommended to disable all debugging in production. During development you can enable the information that you need above."),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}