View source
<?php
namespace Drupal\smart_title_ui\Form;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SmartTitleConfigForm extends ConfigFormBase {
protected $entityTypeManager;
protected $entityFieldManager;
protected $entityTypeBundleInfo;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EntityTypeBundleInfoInterface $entity_type_bunde_info) {
parent::__construct($config_factory);
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeBundleInfo = $entity_type_bunde_info;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('entity_type.bundle.info'));
}
protected function getEditableConfigNames() {
return [
'smart_title.settings',
];
}
public function getFormId() {
return 'smart_title_config_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('smart_title.settings');
$entity_type_definitions = $this->entityTypeManager
->getDefinitions();
$content_entity_type_filter = function (EntityTypeInterface $entity_type_definition) {
return $entity_type_definition instanceof ContentEntityTypeInterface && $entity_type_definition
->entityClassImplements(FieldableEntityInterface::class) && $entity_type_definition
->get('field_ui_base_route');
};
$valid_content_entity_types = array_filter($entity_type_definitions, $content_entity_type_filter);
$entity_bundles = [];
foreach (array_keys($valid_content_entity_types) as $entity_type_id) {
$label_key = $valid_content_entity_types[$entity_type_id]
->getKey('label');
if ($label_key) {
$base_field_definitions = $this->entityFieldManager
->getBaseFieldDefinitions($entity_type_id);
if (!$base_field_definitions[$label_key]
->isDisplayConfigurable('view')) {
$entity_bundles[$entity_type_id]['label'] = $valid_content_entity_types[$entity_type_id]
->getLabel();
$entity_bundles[$entity_type_id]['bundles'] = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
}
}
}
$defaults = $config
->get('smart_title') ?: [];
foreach ($entity_bundles as $type => $definitions) {
$options = [];
$default = [];
foreach ($definitions['bundles'] as $key => $info) {
$options["{$type}:{$key}"] = $info['label'];
if (in_array("{$type}:{$key}", $defaults)) {
$default["{$type}:{$key}"] = "{$type}:{$key}";
}
}
$form[$type . '_bundles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Smart Title for @entity-type', [
'@entity-type' => $definitions['label'],
]),
'#options' => $options,
'#default_value' => $default,
];
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$values = $form_state
->getValues();
$smart_title_bundles_setting = $smart_title_bundles = [];
foreach ($values as $key => $bundle_values) {
if (strpos($key, '_bundles')) {
foreach ($bundle_values as $bundle_key => $bundle_value) {
if ($bundle_value) {
$smart_title_bundles_setting[] = $bundle_key;
}
$smart_title_bundles[] = $bundle_key;
}
}
}
$evd_storage = $this->entityTypeManager
->getStorage('entity_view_display');
$evds = $evd_storage
->loadMultiple();
$not_smart_title_capable_bundles = array_diff($smart_title_bundles, $smart_title_bundles_setting);
foreach ($evds as $evd_id => $evd) {
assert($evd instanceof EntityViewDisplayInterface);
list($target_entity_type_id, $target_bundle) = explode('.', $evd_id);
if (in_array("{$target_entity_type_id}:{$target_bundle}", $not_smart_title_capable_bundles)) {
$evd
->unsetThirdPartySetting('smart_title', 'enabled')
->unsetThirdPartySetting('smart_title', 'settings')
->save();
}
}
$this
->config('smart_title.settings')
->set('smart_title', $smart_title_bundles_setting)
->save();
Cache::invalidateTags([
'entity_field_info',
]);
}
}