diff --git a/config/install/pathauto.settings.yml b/config/install/pathauto.settings.yml index 4ecabd9..6973e91 100644 --- a/config/install/pathauto.settings.yml +++ b/config/install/pathauto.settings.yml @@ -1,3 +1,5 @@ +entity_types: + user: true punctuation: hyphen: 1 verbose : FALSE diff --git a/config/schema/pathauto.schema.yml b/config/schema/pathauto.schema.yml index 0bb126c..c440870 100644 --- a/config/schema/pathauto.schema.yml +++ b/config/schema/pathauto.schema.yml @@ -1,6 +1,11 @@ pathauto.settings: type: config_object mapping: + entity_types: + label: Enabled entity types + type: sequence + sequence: + type: boolean punctuation: type: sequence sequence: diff --git a/pathauto.module b/pathauto.module index d25eba2..af6d7e0 100644 --- a/pathauto.module +++ b/pathauto.module @@ -139,9 +139,8 @@ function pathauto_field_widget_info_alter(&$widgets) { * Implements hook_entity_base_field_info(). */ function pathauto_entity_base_field_info(EntityTypeInterface $entity_type) { - // @todo: Make this configurable and/or remove if - // https://drupal.org/node/476294 is resolved. - if ($entity_type->id() === 'user') { + $config = \Drupal::config('pathauto.settings'); + if ($config->get('entity_types.' . $entity_type->id())) { $fields['path'] = BaseFieldDefinition::create('path') ->setCustomStorage(TRUE) ->setLabel(t('URL alias')) diff --git a/src/Form/PathautoSettingsForm.php b/src/Form/PathautoSettingsForm.php index d6341fe..8f92caa 100644 --- a/src/Form/PathautoSettingsForm.php +++ b/src/Form/PathautoSettingsForm.php @@ -8,10 +8,16 @@ namespace Drupal\pathauto\Form; use Drupal\Component\Utility\SafeMarkup; +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Entity\EntityFieldManagerInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Url; +use Drupal\pathauto\AliasTypeManager; use Drupal\pathauto\PathautoGeneratorInterface; use Drupal\Core\Form\FormStateInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Configure file system settings for this site. @@ -19,6 +25,53 @@ use Drupal\Core\Form\FormStateInterface; class PathautoSettingsForm extends ConfigFormBase { /** + * Case should be left as is in the generated path. + */ + const CASE_LEAVE_ASIS = 0; + + /** + * Case should be lowercased in the generated path. + */ + const CASE_LOWER = 1; + + /** + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** + * @var \Drupal\Core\Entity\EntityFieldManagerInterface + */ + protected $entityFieldManager; + + /** + * @var \Drupal\pathauto\AliasTypeManager + */ + protected $aliasTypeManager; + + /** + * @inheritDoc + */ + public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, AliasTypeManager $alias_type_manager) { + parent::__construct($config_factory); + $this->entityTypeManager = $entity_type_manager; + $this->entityFieldManager = $entity_field_manager; + $this->aliasTypeManager = $alias_type_manager; + } + + /** + * @inheritDoc + */ + 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('plugin.manager.alias_type') + ); + } + + /** * {@inheritdoc} */ public function getFormId() { @@ -38,7 +91,26 @@ class PathautoSettingsForm extends ConfigFormBase { public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('pathauto.settings'); - $form = array(); + $form['entity_types'] = [ + '#type' => 'details', + '#open' => TRUE, + '#title' => $this->t('Enabled entity types'), + '#description' => $this->t('Enable to add a path field and allow to define alias patterns for the given type. Disabled types already define a path field themself.'), + '#tree' => TRUE, + ]; + + // Get all applicable entity types. + foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) { + if (is_subclass_of($entity_type->getClass(), FieldableEntityInterface::class) && $entity_type->hasLinkTemplate('canonical')) { + $field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); + $form['entity_types'][$entity_type_id] = [ + '#type' => 'checkbox', + '#title' => $entity_type->getLabel(), + '#default_value' => isset($field_definitions['path']) || $config->get('entity_types.' . $entity_type_id), + '#disabled' => isset($field_definitions['path']) && $field_definitions['path']->getProvider() != 'pathauto', + ]; + } + } $form['verbose'] = array( '#type' => 'checkbox', @@ -176,12 +248,26 @@ class PathautoSettingsForm extends ConfigFormBase { $config = $this->config('pathauto.settings'); $form_state->cleanValues(); + + $original_entity_types = $config->get('entity_types'); foreach ($form_state->getValues() as $key => $value) { + if ($key == 'entity_types') { + foreach ($value as $entity_type_id => $enabled) { + if (!$enabled || !empty($form['entity_types'][$entity_type_id]['#disabled'])) { + unset($value[$entity_type_id]); + } + } + } $config->set($key, $value); } - $config->save(); + // Clear cached field definitions if the values are changed. + if ($original_entity_types != $config->get('entity_types')) { + $this->entityFieldManager->clearCachedFieldDefinitions(); + $this->aliasTypeManager->clearCachedDefinitions(); + } + parent::submitForm($form, $form_state); } diff --git a/src/Plugin/Deriver/EntityAliasTypeDeriver.php b/src/Plugin/Deriver/EntityAliasTypeDeriver.php index f846d48..b15eb6c 100644 --- a/src/Plugin/Deriver/EntityAliasTypeDeriver.php +++ b/src/Plugin/Deriver/EntityAliasTypeDeriver.php @@ -81,7 +81,6 @@ class EntityAliasTypeDeriver extends DeriverBase implements ContainerDeriverInte if (!isset($base_fields['path'])) { // The entity type does not have a path field and is therefore not // supported. - // @todo: Add a UI to enable that base field on any content entity. continue; } $this->derivatives[$entity_type_id] = $base_plugin_definition;