archivesspace-8.x-1.x-dev/src/Form/ASpaceSettingsForm.php
src/Form/ASpaceSettingsForm.php
<?php
namespace Drupal\archivesspace\Form;
use Drupal\Core\Entity\EntityTypeBundleInfo;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Configure ArchivesSpace Integration settings for this site.
*/
class ASpaceSettingsForm extends ConfigFormBase {
/**
* Config settings.
*
* @var string
*/
const SETTINGS = 'archivesspace.settings';
/**
* State stores connection information.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Bundle information.
*
* @var Drupal\Core\Entity\EntityTypeBundleInfo
*/
protected $bundleInfo;
/**
* For field dropdown selection.
*
* @var Drupal\Core\Entity\EntityFieldManager
*/
protected $fieldManager;
/**
* Contructor.
*
* @param \Drupal\Core\Session\StateInterface $state
* State stores connection info.
* @param \Drupal\Core\Entity\EntityTypeBundleInfor $bundleInfo
* Bundle information.
* @param \Drupal\Core\Entity\EntityFieldManager $fieldManager
* Field manager for populating field drop-down.
*/
public function __construct(StateInterface $state, EntityTypeBundleInfo $bundleInfo, EntityFieldManager $fieldManager) {
$this->state = $state;
$this->bundleInfo = $bundleInfo;
$this->fieldManager = $fieldManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this form class.
return new static(
// Load the service required to construct this class.
$container->get('state'),
$container->get('entity_type.bundle.info'),
$container->get('entity_field.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'archivesspace_admin_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
static::SETTINGS,
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['connection'] = [
'#type' => 'details',
'#title' => $this->t('ArchivesSpace API Connection'),
'#open' => TRUE,
];
$form['connection']['archivesspace_base_uri'] = [
'#type' => 'textfield',
'#title' => $this->t('ArchivesSpace Host & Port'),
'#default_value' => Settings::get('archivespace.base_uri') ?: $this->state->get('archivesspace.base_uri'),
'#disabled' => (bool) Settings::get('archivespace.base_uri'),
'#description' => Settings::get('archivespace.base_uri') ? $this->t('Currently set in Drupal settings (settings.php, local.settings.php, etc).') : "",
];
$form['connection']['archivesspace_api_path'] = [
'#type' => 'textfield',
'#title' => $this->t('ArchivesSpace API Prefix'),
'#default_value' => Settings::get('archivespace.api_path') ?: $this->state->get('archivesspace.api_path'),
'#disabled' => (bool) Settings::get('archivespace.api_path'),
'#description' => Settings::get('archivespace.api_path') ? $this->t('Currently set in Drupal settings (settings.php, local.settings.php, etc).') : $this->t('Optional: the path for the API if it is behind a proxy. E.g. `/staff/api`.'),
];
$form['connection']['archivesspace_username'] = [
'#type' => 'textfield',
'#title' => $this->t('ArchivesSpace Username'),
'#default_value' => $this->state->get('archivesspace.username'),
];
$form['connection']['archivesspace_password'] = [
'#type' => 'password',
'#title' => $this->t('ArchivesSpace Password'),
'#default_value' => '',
'#description' => $this->t('Leave blank to make no changes, use an invalid string to disable if need be.'),
];
$config = $this->config(static::SETTINGS);
$form['resource_description'] = [
'#type' => 'details',
'#title' => $this->t('Resource Description Files'),
'#open' => FALSE,
];
$form['resource_description']['resource_description_queue_enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable Resource Description File Update Queue'),
'#description' => $this->t("Allow resource description and archival object updates can trigger resource description file update requests. Note: We ignore update requests that were made before the resource description file's most recent update. This prevents redundant updates."),
'#default_value' => $config->get('resource_description_queue_enabled'),
];
$form['breadcrumbs'] = [
'#type' => 'details',
'#title' => $this->t('Breadcrumbs'),
'#description' => $this->t('ArchivesSpace breadcrumbs show the parent hierarchy of archival objects and are usually displayed across the top of a page.'),
'#open' => FALSE,
];
foreach ($this->bundleInfo->getBundleInfo('node') as $content_type => $content_type_properties) {
$content_type_options[$content_type] = $content_type_properties['label'];
}
$form['breadcrumbs']['breadcrumb_content_types'] = [
'#title' => $this->t('ArchivesSpace Content Types'),
'#type' => 'checkboxes',
'#options' => $content_type_options,
'#default_value' => $config->get('breadcrumb.content_types'),
'#description' => $this->t('Select which content types represent ArchivesSpace resources and archival objects. Deselecting them effectively disables this breadcrumb service allowing either the system-provided breadcrumbs or other breadcrumb generators to be used instead.'),
];
$field_map = $this->fieldManager->getFieldMapByFieldType('entity_reference');
$node_fields = array_keys($field_map['node']);
$parent_options = array_combine($node_fields, $node_fields);
$form['breadcrumbs']['parent_field'] = [
'#type' => 'select',
'#title' => $this->t('Field that contains reference to parents'),
'#options' => $parent_options,
'#empty_option' => $this->t('None'),
'#default_value' => $config->get('breadcrumb.parent_field'),
'#description' => $this->t("Machine name of field that contains references to parent node."),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Set the provided values in Drupal state if not configured in settings.
if (!Settings::get('archivespace.base_uri')) {
$state->set('archivesspace.base_uri', $form_state->getValue('archivesspace_base_uri'));
}
if (!Settings::get('archivespace.api_path')) {
$state->set('archivesspace.api_path', $form_state->getValue('archivesspace_api_path'));
}
$this->state->set('archivesspace.username', $form_state->getValue('archivesspace_username'));
if (!empty($form_state->getValue('archivesspace_password'))) {
$this->state->set('archivesspace.password', $form_state->getValue('archivesspace_password'));
}
$this->configFactory->getEditable(static::SETTINGS)
->set('breadcrumb.content_types', array_keys(array_filter($form_state->getValue('breadcrumb_content_types'))))
->set('breadcrumb.parent_field', $form_state->getValue('parent_field'))
->set('resource_description_queue_enabled', $form_state->getValue('resource_description_queue_enabled'))
->save();
parent::submitForm($form, $form_state);
}
}
