View source
<?php
namespace Drupal\civicrm_entity\Form;
use Drupal\civicrm_entity\SupportedEntities;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\LocalActionManagerInterface;
use Drupal\Core\Menu\LocalTaskManagerInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
use Drupal\filter\FilterFormatInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CivicrmEntitySettings extends ConfigFormBase {
protected $entityTypeManager;
protected $routeBuilder;
protected $localActionManager;
protected $localTaskManager;
protected $menuLinkManager;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, RouteBuilderInterface $route_builder, LocalActionManagerInterface $local_action_manager, LocalTaskManagerInterface $local_task_manager, MenuLinkManagerInterface $menu_link_manager) {
parent::__construct($config_factory);
$this->entityTypeManager = $entity_type_manager;
$this->routeBuilder = $route_builder;
$this->localActionManager = $local_action_manager;
$this->localTaskManager = $local_task_manager;
$this->menuLinkManager = $menu_link_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('router.builder'), $container
->get('plugin.manager.menu.local_action'), $container
->get('plugin.manager.menu.local_task'), $container
->get('plugin.manager.menu.link'));
}
protected function getEditableConfigNames() {
return [
'civicrm_entity.settings',
];
}
public function getFormId() {
return 'civicrm_entity_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$config = $this
->config('civicrm_entity.settings');
$formats = filter_formats();
$form['filter_format'] = [
'#type' => 'select',
'#title' => $this
->t('Text format'),
'#options' => array_map(function (FilterFormatInterface $filter) {
return $filter
->label();
}, $formats),
'#default_value' => $config
->get('filter_format') ?: filter_fallback_format(),
'#access' => count($formats) > 1 && $this
->currentUser()
->hasPermission('administer filters'),
'#attributes' => [
'class' => [
'filter-list',
],
],
];
$civicrm_entity_types = SupportedEntities::getInfo();
$options = array_map(function (array $entity_info) {
return $entity_info['civicrm entity label'];
}, $civicrm_entity_types);
asort($options);
$form['enabled_entity_types'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Enabled entity types'),
'#options' => $options,
'#default_value' => $config
->get('enabled_entity_types'),
];
$form['advanced_settings'] = [
'#type' => 'details',
'#title' => $this
->t('Advanced settings'),
'#open' => FALSE,
];
$form['advanced_settings']['disable_hooks'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Disable pre/post hooks'),
'#default_value' => $config
->get('disable_hooks'),
'#description' => $this
->t('Not intended for normal use. Provided to temporarily disable Drupal entity hooks for CiviCRM Entity types for special cases, such as migrations. Only disable if you know you need to.'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$enabled_entity_type = array_filter($form_state
->getValue('enabled_entity_types'));
$this
->config('civicrm_entity.settings')
->set('filter_format', $form_state
->getValue('filter_format'))
->set('enabled_entity_types', $enabled_entity_type)
->set('disable_hooks', $form_state
->getValue('disable_hooks'))
->save();
$this->entityTypeManager
->clearCachedDefinitions();
$this->routeBuilder
->rebuild();
$this->localActionManager
->clearCachedDefinitions();
$this->localTaskManager
->clearCachedDefinitions();
}
}