View source
<?php
namespace Drupal\acquia_lift\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\Config;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\node\Entity\NodeType;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\acquia_lift\Service\Helper\SettingsHelper;
class AdminSettingsForm extends ConfigFormBase {
private $entityManager;
public function getFormId() {
return 'acquia_lift_settings_form';
}
protected function getEditableConfigNames() {
return [
'acquia_lift.settings',
];
}
public function __construct(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'));
}
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$form['credential'] = $this
->buildCredentialForm();
$form['data_collection_settings'] = [
'#type' => 'vertical_tabs',
'#title' => t('Data collection settings'),
];
$form['identity'] = $this
->buildIdentityForm();
$form['field_mappings'] = $this
->buildFieldMappingsForm();
$form['visibility'] = $this
->buildVisibilityForm();
$form['thumbnail_url'] = $this
->buildThumbnailUrlForm();
return parent::buildForm($form, $form_state);
}
private function buildCredentialForm() {
$credential_settings = $this
->config('acquia_lift.settings')
->get('credential');
$form = [
'#title' => t('Credential'),
'#type' => 'details',
'#tree' => TRUE,
'#open' => SettingsHelper::isInvalidCredential($credential_settings),
];
$form['account_name'] = [
'#type' => 'textfield',
'#title' => t('Account Name'),
'#default_value' => $credential_settings['account_name'],
'#required' => TRUE,
];
$form['customer_site'] = [
'#type' => 'textfield',
'#title' => t('Customer Site'),
'#default_value' => $credential_settings['customer_site'],
];
$form['api_url'] = [
'#type' => 'textfield',
'#title' => t('API URL'),
'#field_prefix' => 'http(s)://',
'#default_value' => $credential_settings['api_url'],
'#required' => TRUE,
];
$form['access_key'] = [
'#type' => 'textfield',
'#title' => t('API Access Key'),
'#default_value' => $credential_settings['access_key'],
'#required' => TRUE,
];
$form['secret_key'] = [
'#type' => 'password',
'#title' => t('API Secret Key'),
'#default_value' => $credential_settings['secret_key'],
'#required' => empty($credential_settings['secret_key']),
'#description' => !empty($credential_settings['secret_key']) ? t('Only necessary if updating') : '',
];
$form['js_path'] = [
'#type' => 'textfield',
'#title' => t('JavaScript Path'),
'#field_prefix' => 'http(s)://',
'#default_value' => $credential_settings['js_path'],
'#required' => TRUE,
];
return $form;
}
private function buildIdentityForm() {
$identity_settings = $this
->config('acquia_lift.settings')
->get('identity');
$form = [
'#title' => t('Identity'),
'#type' => 'details',
'#tree' => TRUE,
'#group' => 'data_collection_settings',
];
$form['identity_parameter'] = [
'#type' => 'textfield',
'#title' => t('Identity Parameter'),
'#default_value' => $identity_settings['identity_parameter'],
];
$form['identity_type_parameter'] = [
'#type' => 'textfield',
'#title' => t('Identity Type Parameter'),
'#default_value' => $identity_settings['identity_type_parameter'],
'#states' => [
'visible' => [
':input[name="identity[identity_parameter]"]' => [
'!value' => '',
],
],
],
];
$form['default_identity_type'] = [
'#type' => 'textfield',
'#title' => t('Default Identity Type'),
'#default_value' => $identity_settings['default_identity_type'],
'#placeholder' => SettingsHelper::DEFAULT_IDENTITY_TYPE_DEFAULT,
];
return $form;
}
private function buildFieldMappingsForm() {
$field_mappings_settings = $this
->config('acquia_lift.settings')
->get('field_mappings');
$field_names = $this
->getTaxonomyTermFieldNames();
$form = [
'#title' => t('Field Mappings'),
'#type' => 'details',
'#tree' => TRUE,
'#group' => 'data_collection_settings',
];
$form['content_section'] = [
'#type' => 'select',
'#title' => t('Content Section'),
'#empty_value' => '',
'#options' => $field_names,
'#default_value' => $field_mappings_settings['content_section'],
];
$form['content_keywords'] = [
'#type' => 'select',
'#title' => t('Content Keywords'),
'#empty_value' => '',
'#options' => $field_names,
'#default_value' => $field_mappings_settings['content_keywords'],
];
$form['persona'] = [
'#type' => 'select',
'#title' => t('Persona'),
'#empty_value' => '',
'#options' => $field_names,
'#default_value' => $field_mappings_settings['persona'],
];
return $form;
}
private function getTaxonomyTermFieldNames() {
$definitions = $this->entityManager
->getFieldStorageDefinitions('node');
$field_names = [];
foreach ($definitions as $field_name => $field_storage) {
if ($field_storage
->getType() != 'entity_reference' || $field_storage
->getSetting('target_type') !== 'taxonomy_term') {
continue;
}
$field_names[$field_name] = $field_name;
}
return $field_names;
}
private function buildVisibilityForm() {
$visibility_settings = $this
->config('acquia_lift.settings')
->get('visibility');
$form = [
'#title' => t('Visibility'),
'#type' => 'details',
'#tree' => TRUE,
'#group' => 'data_collection_settings',
];
$form['path_patterns'] = [
'#type' => 'textarea',
'#title' => t('Path patterns'),
'#description' => t('Lift will skip data collection on those URLs and their aliases.'),
'#default_value' => $visibility_settings['path_patterns'],
];
return $form;
}
private function buildThumbnailUrlForm() {
$form = [
'#title' => t('Thumbnail URL'),
'#type' => 'details',
'#tree' => TRUE,
'#group' => 'data_collection_settings',
];
$form['link_list'] = [
'#type' => 'markup',
'#markup' => '<div>' . t('There are no content types. Please create a content type first.') . '</div>',
];
$node_types = NodeType::loadMultiple();
if (empty($node_types)) {
return $form;
}
$links = [];
$link_attributes = [
'attributes' => [
'target' => '_blank',
],
'fragment' => 'edit-acquia-lift',
];
foreach ($node_types as $node_type) {
$url = Url::fromRoute('entity.node_type.edit_form', [
'node_type' => $node_type
->id(),
], $link_attributes);
$links[] = '<p>' . Link::fromTextAndUrl($node_type
->label(), $url)
->toString() . '</p>';
}
$form['link_list']['#markup'] = t('Configure thumbnail URLs on each content type\'s edit page (in a new window):');
$form['link_list']['#markup'] .= implode('', $links);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$settings = $this
->config('acquia_lift.settings');
$values = $form_state
->getValues();
$this
->setCredentialValues($settings, $values['credential']);
$this
->setIdentityValues($settings, $values['identity']);
$this
->setFieldMappingsValues($settings, $values['field_mappings']);
$this
->setVisibilityValues($settings, $values['visibility']);
$settings
->save();
drupal_flush_all_caches();
parent::submitForm($form, $form_state);
}
private function setCredentialValues(Config $settings, array $values) {
$settings
->set('credential.account_name', $values['account_name']);
$settings
->set('credential.customer_site', $values['customer_site']);
$settings
->set('credential.api_url', $this
->removeProtocal($values['api_url']));
$settings
->set('credential.access_key', $values['access_key']);
if (!empty($values['secret_key'])) {
$settings
->set('credential.secret_key', $values['secret_key']);
}
$settings
->set('credential.js_path', $this
->removeProtocal($values['js_path']));
}
private function removeProtocal($url) {
return preg_replace('~^https?://~', '', $url);
}
private function setIdentityValues(Config $settings, array $values) {
$settings
->set('identity.identity_parameter', $values['identity_parameter']);
$settings
->set('identity.identity_type_parameter', $values['identity_type_parameter']);
$settings
->set('identity.default_identity_type', $values['default_identity_type']);
}
private function setFieldMappingsValues(Config $settings, array $values) {
$settings
->set('field_mappings.content_section', $values['content_section']);
$settings
->set('field_mappings.content_keywords', $values['content_keywords']);
$settings
->set('field_mappings.persona', $values['persona']);
}
private function setVisibilityValues(Config $settings, array $values) {
$settings
->set('visibility.path_patterns', $values['path_patterns']);
}
}