diff --git a/config/install/rabbit_hole.behavior_settings.default.yml b/config/install/rabbit_hole.behavior_settings.default.yml new file mode 100644 index 0000000..446c8b4 --- /dev/null +++ b/config/install/rabbit_hole.behavior_settings.default.yml @@ -0,0 +1,7 @@ +id: default +action: "display_page" +# Allow +allow_override: 1 +# N/A +redirect_code: 0 +redirect_path: '' diff --git a/modules/rh_node/rh_node.module b/modules/rh_node/rh_node.module index e8843da..5c3fce3 100644 --- a/modules/rh_node/rh_node.module +++ b/modules/rh_node/rh_node.module @@ -9,8 +9,8 @@ function rh_node_form_node_type_form_alter(&$form, $form_state) { // Add the Rabbit Hole form, and add an extra javascript file that's needed // for the fieldset summary. - \Drupal::service('rabbit_hole.form_mangler')->addRabbitHoleOptionsToForm( - $form, 'rh_node', $form_state->getFormObject()->getEntity()); + // \Drupal::service('rabbit_hole.form_mangler')->addRabbitHoleOptionsToEntityForm( + // $form, 'rh_node', $form_state->getFormObject()->getEntity()); $form['#attached']['library'][] = 'rh_node/node-form'; } @@ -23,20 +23,14 @@ function rh_node_form_node_type_form_alter(&$form, $form_state) { function rh_node_form_node_form_alter(&$form, $form_state) { // Add the Rabbit Hole form, and add an extra javascript file that's needed // for the fieldset summary. - \Drupal::service('rabbit_hole.form_mangler')->addRabbitHoleOptionsToForm( - $form, 'rh_node', $form_state->getFormObject()->getEntity()); - $form['#attached']['library'][] = 'rh_node/node-form'; + // \Drupal::service('rabbit_hole.form_mangler')->addRabbitHoleOptionsToEntityForm( + // $form, 'rh_node', $form_state->getFormObject()->getEntity()); + // $form['#attached']['library'][] = 'rh_node/node-form'; - $form_state->setSubmitHandlers( - array_merge($form_state->getSubmitHandlers(), - array('_rabbit_hole_general_form_submit')) - ); - // dpm('[#submit]'); - // dpm($form['#submit']); - // dpm('[actions][submit][#submit]'); - // dpm($form['actions']['submit']['#submit']); - // dpm('[actions][publish][#submit]'); - // dpm($form['actions']['publish']['#submit']); + // $form_state->setSubmitHandlers( + // array_merge($form_state->getSubmitHandlers(), + // array('_rabbit_hole_general_form_submit')) + // ); // Add a custom submit function. This is used to disable the redirect to // node/123 if Rabbit Hole is enabled and the user doesn't have the bypass @@ -45,3 +39,8 @@ function rh_node_form_node_form_alter(&$form, $form_state) { // $form['actions']['submit']['#submit'][] = 'rh_node_node_form_submit'; // } } + +function rh_node_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) { + return \Drupal::service('rabbit_hole.entity_extender') + ->getRabbitHoleFields('node'); +} diff --git a/modules/rh_node/src/Plugin/RabbitHoleEntityPlugin/Node.php b/modules/rh_node/src/Plugin/RabbitHoleEntityPlugin/Node.php new file mode 100644 index 0000000..810474a --- /dev/null +++ b/modules/rh_node/src/Plugin/RabbitHoleEntityPlugin/Node.php @@ -0,0 +1,32 @@ +addRabbitHoleOptionsToEntityForm( +// $form, 'rh_user', $form_state->getFormObject()->getEntity()); +// } + +// function rh_user_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { +// switch ($form_id) { + // case 'user_form': + // case 'user_register_form': + // \Drupal::service('rabbit_hole.form_mangler')->addRabbitHoleOptionsToEntityForm( + // $form, 'rh_user', $form_state->getFormObject()->getEntity()); + // break; + // case 'user_admin_settings': + // break; +// } +//} + +function rh_user_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) { + return \Drupal::service('rabbit_hole.entity_extender') + ->getRabbitHoleFields('user'); +} + +/** + * Implements hook_form_FORM_ID_alter(). + * + * This will add Rabbit Hole options to the user form. These settings will + * be used as default for every node of this node type. + */ +// function rh_user_form_user_profile_form_alter(&$form, $form_state) { +// // Add the Rabbit Hole form, and add an extra javascript file that's needed +// // for the fieldset summary. +// \Drupal::service('rabbit_hole.form_mangler')->addRabbitHoleOptionsToEntityForm( +// $form, 'rh_user', $form_state->getFormObject()->getEntity()); +// } + +/** + * Implements hook_form_FORM_ID_alter(). + * + * This will add Rabbit Hole options to the node form. The user will be able to + * override the default Rabbit Hole options. + */ +// function rh_user_form_user_register_form_alter(&$form, $form_state) { + // Add the Rabbit Hole form, and add an extra javascript file that's needed + // for the fieldset summary. + // \Drupal::service('rabbit_hole.form_mangler')->addRabbitHoleOptionsToEntityForm( + // $form, 'rh_node', $form_state->getFormObject()->getEntity()); + + // $form_state->setSubmitHandlers( + // array_merge($form_state->getSubmitHandlers(), + // array('_rabbit_hole_general_form_submit')) + // ); + + // Add a custom submit function. This is used to disable the redirect to + // node/123 if Rabbit Hole is enabled and the user doesn't have the bypass + // rh_node permission. + // if (!user_access('bypass rh_node')) { + // $form['actions']['submit']['#submit'][] = 'rh_node_node_form_submit'; + // } +// } diff --git a/modules/rh_user/src/Plugin/RabbitHoleEntityPlugin/User.php b/modules/rh_user/src/Plugin/RabbitHoleEntityPlugin/User.php new file mode 100644 index 0000000..9a7960f --- /dev/null +++ b/modules/rh_user/src/Plugin/RabbitHoleEntityPlugin/User.php @@ -0,0 +1,45 @@ +id() in_array($entity_types)) { - if ($entity_type->id() == 'node') { - return \Drupal::service('rabbit_hole.entity_extender') - ->getGeneralExtraFields(); - } -} - -/** * Implements hook_theme(). */ function rabbit_hole_theme() { @@ -55,3 +41,40 @@ function _rabbit_hole_general_form_submit($form, $form_state) { \Drupal::service('rabbit_hole.form_mangler') ->handleFormSubmit($form, $form_state); } + +/** + * Implements hook_form_alter(). + */ +function rabbit_hole_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { + $affected_types = \Drupal::service('plugin.manager.rabbit_hole_entity_plugin') + ->loadSupportedEntityTypes(); + $affected_bundle_types = \Drupal::service('plugin.manager.rabbit_hole_entity_plugin') + ->loadSupportedBundleEntityTypes(); + $affected_global_forms = \Drupal::service('plugin.manager.rabbit_hole_entity_plugin') + ->loadSupportedGlobalForms(); + if (method_exists($form_state->getFormObject(), 'getEntity')) { + $current_type = $form_state->getFormObject()->getEntity()->getEntityTypeId(); + if (in_array($current_type, $affected_types)) { + \Drupal::service('rabbit_hole.form_mangler')->addRabbitHoleOptionsToEntityForm( + $form, $form_state->getFormObject()->getEntity()); + + $form_state->setSubmitHandlers( + array_merge($form_state->getSubmitHandlers(), + array('_rabbit_hole_general_form_submit')) + ); + } + else if (in_array($current_type, $affected_bundle_types)) { + \Drupal::service('rabbit_hole.form_mangler')->addRabbitHoleOptionsToEntityForm( + $form, $form_state->getFormObject()->getEntity()); + $form_state->setSubmitHandlers( + array_merge($form_state->getSubmitHandlers(), + array('_rabbit_hole_general_form_submit')) + ); + } + } + else if (array_key_exists($form_id, $affected_global_forms)) { + \Drupal::service('rabbit_hole.form_mangler')->addRabbitHoleOptionsToGlobalForm( + $form, $affected_global_forms[$form_id] + ); + } +} diff --git a/rabbit_hole.services.yml b/rabbit_hole.services.yml index 0a87a9e..fd93601 100644 --- a/rabbit_hole.services.yml +++ b/rabbit_hole.services.yml @@ -1,4 +1,9 @@ services: + plugin.manager.rabbit_hole_entity_plugin: + class: Drupal\rabbit_hole\Plugin\RabbitHoleEntityPluginManager + parent: default_plugin_manager + arguments: + - '@entity_type.manager' plugin.manager.rabbit_hole_behavior_plugin: class: Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPluginManager parent: default_plugin_manager @@ -11,6 +16,7 @@ services: - '@entity_type.manager' - '@entity_type.bundle.info' - '@plugin.manager.rabbit_hole_behavior_plugin' + - "@plugin.manager.rabbit_hole_entity_plugin" - '@rabbit_hole.behavior_settings_manager' - "@string_translation" rabbit_hole.behavior_invoker: @@ -18,15 +24,15 @@ services: arguments: - "@rabbit_hole.behavior_settings_manager" - "@plugin.manager.rabbit_hole_behavior_plugin" + - "@plugin.manager.rabbit_hole_entity_plugin" - "@rabbit_hole.entity_extender" - rabbit_hole.rabbit_hole_subscriber: class: Drupal\rabbit_hole\EventSubscriber\RabbitHoleSubscriber arguments: ["@rabbit_hole.behavior_invoker"] tags: - { name: event_subscriber } - rabbit_hole.entity_extender: class: Drupal\rabbit_hole\EntityExtender - arguments: ["@plugin.manager.rabbit_hole_behavior_plugin"] - + arguments: + - "@plugin.manager.rabbit_hole_behavior_plugin" + - "@plugin.manager.rabbit_hole_entity_plugin" diff --git a/src/Annotation/RabbitHoleEntityPlugin.php b/src/Annotation/RabbitHoleEntityPlugin.php new file mode 100644 index 0000000..60c4a60 --- /dev/null +++ b/src/Annotation/RabbitHoleEntityPlugin.php @@ -0,0 +1,44 @@ +rhBehaviorSettingsManager = $rabbit_hole_behavior_settings_manager; $this->rhBehaviorPluginManager = $plugin_manager_rabbit_hole_behavior_plugin; + $this->rhEntityPluginManager = $plugin_manager_rabbit_hole_entity_plugin; $this->rhEntityExtender = $entity_extender; } @@ -93,12 +104,17 @@ class BehaviorInvoker implements BehaviorInvokerInterface { } /** - * @todo Load this from plugins + * Load a list of entity IDs supported by rabbit hole based on available + * plugins + * @return array + * An array of string entity ids */ - public function getPossibleEntityKeys() { - return array( - 'node' - ); + public function getPossibleEntityTypeKeys() { + $entity_type_keys = array(); + foreach ($this->rhEntityPluginManager->getDefinitions() as $id => $def) { + $entity_type_keys[] = $def['entity_type']; + } + return $entity_type_keys; } /** @@ -121,7 +137,8 @@ class BehaviorInvoker implements BehaviorInvokerInterface { if ($trigger_default_bundle_action) { $config = $this->rhBehaviorSettingsManager->loadBehaviorSettingsAsConfig( - $entity->getEntityType()->getBundleEntityType(), + $entity->getEntityType()->getBundleEntityType() + ?: $entity->getEntityType()->id(), $entity->bundle()); foreach ($field_keys as $field_key) { diff --git a/src/BehaviorSettingsManager.php b/src/BehaviorSettingsManager.php index 16b6807..a59a081 100644 --- a/src/BehaviorSettingsManager.php +++ b/src/BehaviorSettingsManager.php @@ -35,11 +35,9 @@ class BehaviorSettingsManager implements BehaviorSettingsManagerInterface { /** * {@inheritdoc} */ - public function saveBehaviorSettings($settings, $entity_type_id, $entity_id, - $is_bundle = FALSE) { + public function saveBehaviorSettings($settings, $entity_type_id, $entity_id = NULL) { - $id = $this->generateBehaviorSettingsFullId($entity_type_id, $entity_id, - $is_bundle); + $id = $this->generateBehaviorSettingsFullId($entity_type_id, $entity_id); $entity = BehaviorSettings::load($id); if ($entity === NULL) { @@ -69,7 +67,7 @@ class BehaviorSettingsManager implements BehaviorSettingsManagerInterface { } else { return $this->config_factory - ->get('rabbit_hole.behavior_settings.default_bundle'); + ->get('rabbit_hole.behavior_settings.default'); } } diff --git a/src/EntityExtender.php b/src/EntityExtender.php index 52d81c3..2961ada 100644 --- a/src/EntityExtender.php +++ b/src/EntityExtender.php @@ -10,6 +10,7 @@ namespace Drupal\rabbit_hole; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPluginManager; +use Drupal\rabbit_hole\Plugin\RabbitHoleEntityPluginManager; /** * Class EntityExtender. @@ -27,10 +28,30 @@ class EntityExtender implements EntityExtenderInterface { protected $rhBehaviorPluginManager; /** + * Drupal\rabbit_hole\Plugin\RabbitHoleEntityPluginManager definition. + * + * @var Drupal\rabbit_hole\Plugin\RabbitHoleEntityPluginManager + */ + protected $rhEntityPluginManager; + + /** * Constructor. */ - public function __construct(RabbitHoleBehaviorPluginManager $plugin_manager_rabbit_hole_behavior_plugin) { + public function __construct(RabbitHoleBehaviorPluginManager $plugin_manager_rabbit_hole_behavior_plugin, + RabbitHoleEntityPluginManager $plugin_manager_rabbit_hole_entity_plugin) { $this->rhBehaviorPluginManager = $plugin_manager_rabbit_hole_behavior_plugin; + $this->rhEntityPluginManager = $plugin_manager_rabbit_hole_entity_plugin; + } + + /** + * Return fields that rabbit hole adds for use in entity_base_field_info hooks + * @param string $entity_type_id The string ID of the entity type + */ + public function getRabbitHoleFields($entity_type_id) { + $entity_types = $this->rhEntityPluginManager->loadSupportedEntityTypes(); + if (in_array($entity_type_id, $entity_types)) { + return $this->getGeneralExtraFields(); + } } /** @@ -39,6 +60,7 @@ class EntityExtender implements EntityExtenderInterface { public function getGeneralExtraFields() { $fields = array(); $fields['rh_action'] = BaseFieldDefinition::create('string') + ->setName('rh_action') ->setLabel($this->t('Rabbit Hole action')) ->setDescription($this->t('Specifies which action that Rabbit Hole should take.')); foreach ($this->rhBehaviorPluginManager->getDefinitions() as $id => $def) { diff --git a/src/EventSubscriber/RabbitHoleSubscriber.php b/src/EventSubscriber/RabbitHoleSubscriber.php index a6a8066..7398a8a 100644 --- a/src/EventSubscriber/RabbitHoleSubscriber.php +++ b/src/EventSubscriber/RabbitHoleSubscriber.php @@ -66,13 +66,19 @@ class RabbitHoleSubscriber implements EventSubscriberInterface { * Process events generically invoking rabbit hole behaviors if necessary */ private function processEvent(Event $event) { + // Don't process events with HTTP exceptions - those have either been thrown + // by us or have nothing to do with rabbit hole + if ($event->getRequest()->get('exception') != NULL) { + return; + } + // We won't go ahead if we have an entity form (i.e. we're adding/editing // an entity) if ($event->getRequest()->get('_entity_form') == NULL) { // We check for all of our known entity keys that work with rabbit hole // and invoke rabbit hole behavior on the first one we find (which // should also be the only one) - $entity_keys = $this->rabbitHoleBehaviorInvoker->getPossibleEntityKeys(); + $entity_keys = $this->rabbitHoleBehaviorInvoker->getPossibleEntityTypeKeys(); foreach ($entity_keys as $ekey) { $entity = $event->getRequest()->get($ekey); if (isset($entity)) { diff --git a/src/FormManglerService.php b/src/FormManglerService.php index e15a71b..45a5409 100644 --- a/src/FormManglerService.php +++ b/src/FormManglerService.php @@ -15,6 +15,7 @@ use Drupal\Core\Entity\ContentEntityType; use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\Entity\EntityTypeBundleInfo; use Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPluginManager; +use Drupal\rabbit_hole\Plugin\RabbitHoleEntityPluginManager; use Drupal\rabbit_hole\BehaviorSettingsManager; use Drupal\rabbit_hole\Entity\BehaviorSettings; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -33,6 +34,7 @@ class FormManglerService { private $entityTypeManager = NULL; private $bundleInfo = NULL; private $rhBehaviorPluginManager = NULL; + private $rhEntityPluginManager = NULL; /** * Constructor. @@ -41,17 +43,33 @@ class FormManglerService { EntityTypeManager $etm, EntityTypeBundleInfo $etbi, RabbitHoleBehaviorPluginManager $behaviorPluginManager, + RabbitHoleEntityPluginManager $entityPluginManager, BehaviorSettingsManager $behaviorSettingsManager, TranslationInterface $translation) { $this->entityTypeManager = $etm; $this->allBundleInfo = $etbi->getAllBundleInfo(); $this->rhBehaviorPluginManager = $behaviorPluginManager; + $this->rhEntityPluginManager = $entityPluginManager; $this->rhBehaviorSettingsManager = $behaviorSettingsManager; $this->stringTranslation = $translation; } - // Add common methods and abstract methods for your plugin type here. + /** + * Add rabbit hole options to the global configuration form for an entity type + * (e.g. options for all users) + * + * @param array $attach + * The form that the Rabbit Hole form should be attached to. + * @param string $entity_type + * The name of the entity for which this form provides global options + */ + public function addRabbitHoleOptionsToGlobalForm(array &$attach, $entity_type) { + $entity_type = $this->entityTypeManager->getStorage($entity_type) + ->getEntityType(); + + $this->addRabbitHoleOptionsToForm($attach, $entity_type->id()); + } /** * Form structure for the Rabbit Hole configuration. @@ -61,37 +79,63 @@ class FormManglerService { * * @param array $attach * The form that the Rabbit Hole form should be attached to. - * @param string $entity_type - * The entity type that we're adding the form for, e.g. 'node'. - * @param string $bundle - * The bundle that we're adding the form to, e.g. the content type for nodes. - * This might be an empty string if we're creating a new bundle. - * @param string $module - * The name of the module that invokes this function. * @param object $entity * The entity that we're adding the form to, e.g. a node. This should be * defined even in the case of bundles since it is used to determine bundle * and entity type. */ - public function addRabbitHoleOptionsToForm(array &$attach, $module, Entity $entity) { - $bundle = $entity->bundle(); - $entity_type = $entity->getEntityType(); - $entity_is_bundle = $this->isEntityBundle($entity); + public function addRabbitHoleOptionsToEntityForm(array &$attach, Entity $entity) { + $this->addRabbitHoleOptionsToForm($attach, $entity->getEntityType()->id(), + $entity); + } - $bundle_settings = NULL; + /** + * Common functionality for adding rabbit hole options to forms + * + * @param array $attach + * The form that the Rabbit Hole form should be attached to. + * @param string $entity_type_id + * The string ID of the entity type for the form, e.g. 'node' + * @param object $entity + * The entity that we're adding the form to, e.g. a node. This should be + * defined even in the case of bundles since it is used to determine bundle + * and entity type. + */ + private function addRabbitHoleOptionsToForm(array &$attach, + $entity_type_id, $entity = NULL) { + $entity_type = $this->entityTypeManager->getStorage($entity_type_id) + ->getEntityType(); + + $is_bundle_or_entity_type = FALSE; + if ($entity === NULL) { + $is_bundle_or_entity_type = TRUE; + } + else { + $is_bundle_or_entity_type = $this->isEntityBundle($entity); + } + + $bundle_settings = NULL; + $bundle = isset($entity) ? $entity->bundle() : $entity_type_id; $action = NULL; - if ($entity_is_bundle) { - $bundle_settings = $this->rhBehaviorSettingsManager - ->loadBehaviorSettingsAsConfig($entity_type->id(), - $entity->id()); + if ($is_bundle_or_entity_type) { + if ($entity === NULL) { + $bundle_settings = $this->rhBehaviorSettingsManager + ->loadBehaviorSettingsAsConfig($entity_type->id()); + } + else { + $bundle_settings = $this->rhBehaviorSettingsManager + ->loadBehaviorSettingsAsConfig($entity_type->id(), $entity->id()); + } $action = $bundle_settings->get('action'); } else { + $bundle_entity_type = $entity_type->getBundleEntityType() + ?: $entity_type->id(); $bundle_settings = $this->rhBehaviorSettingsManager - ->loadBehaviorSettingsAsConfig($entity_type->getBundleEntityType(), + ->loadBehaviorSettingsAsConfig($bundle_entity_type, $entity->bundle()); $action = isset($entity->rh_action->value) @@ -105,6 +149,9 @@ class FormManglerService { : NULL; } + $entity_plugin = $this->rhEntityPluginManager->createInstanceByEntityType( + $entity_type_id); + // if (!user_access('administer ' . $module)) { // The user doesn't have access, exit. // return; @@ -149,23 +196,32 @@ class FormManglerService { * files will work in the same way and even if they do later * entities might not */ - '#group' => $entity_is_bundle ? 'additional_settings' : 'advanced', + '#group' => $is_bundle_or_entity_type ? 'additional_settings' : 'advanced', '#attributes' => array('class' => array('rabbit-hole-settings-form')), ); // $form['path']['#group'] = 'rabbit_hole'; // Add the invoking module to the internal values. - $form['rabbit_hole']['rh_module'] = array( - '#type' => 'value', - '#value' => $module, - ); + /** + * @todo This can probably be removed - check + */ +// $form['rabbit_hole']['rh_module'] = array( +// '#type' => 'value', +// '#value' => $module, +// ); + $form['rabbit_hole']['rh_is_bundle'] = array( '#type' => 'hidden', - '#value' => $entity_is_bundle, + '#value' => $is_bundle_or_entity_type, + ); + + $form['rabbit_hole']['rh_entity_type'] = array( + '#type' => 'hidden', + '#value' => $entity_type->id(), ); // Add override setting if we're editing a bundle. - if ($entity_is_bundle) { + if ($is_bundle_or_entity_type) { $allow_override = $bundle_settings->get('allow_override'); $form['rabbit_hole']['rh_override'] = array( '#type' => 'checkbox', @@ -178,7 +234,7 @@ class FormManglerService { // Add action setting. $action_options = $this->loadBehaviorOptions(); - if (!$entity_is_bundle) { + if (!$is_bundle_or_entity_type) { // Add an option if we are editing an entity. This will allow us to use the // configuration for the bundle. $action_bundle = $bundle_settings->get('action'); @@ -197,7 +253,7 @@ class FormManglerService { ); $this->populateExtraBehaviorSections($form, NULL, NULL, $entity, - $entity_is_bundle, $bundle_settings); + $is_bundle_or_entity_type, $bundle_settings); /** * @todo Add redirect settings @@ -310,8 +366,19 @@ class FormManglerService { /** * @todo This should probably be moved out into plugins based on entity type */ - $attach['actions']['submit']['#submit'][] = '_rabbit_hole_general_form_submit'; - $attach['actions']['publish']['#submit'][] = '_rabbit_hole_general_form_submit'; + // $attach['actions']['submit']['#submit'][] = '_rabbit_hole_general_form_submit'; + // $attach['actions']['publish']['#submit'][] = '_rabbit_hole_general_form_submit'; + + $submit_handler_locations = $entity_plugin + ->getFormSubmitHandlerAttachLocations(); + + foreach($submit_handler_locations as $location) { + $array_ref = &$attach; + foreach ($location as $subkey) { + $array_ref = &$array_ref[$subkey]; + } + $array_ref[] = '_rabbit_hole_general_form_submit'; + } /** * @todo Optionally provide additional form submission handler (can we do this via plugin?) @@ -330,7 +397,10 @@ class FormManglerService { */ public function handleFormSubmit($form, $form_state) { if ($form_state->getValue('rh_is_bundle')) { - $entity = $form_state->getFormObject()->getEntity(); + $entity = NULL; + if (method_exists($form_state->getFormObject(), 'getEntity')) { + $entity = $form_state->getFormObject()->getEntity(); + } $allow_override = $form_state->getValue('rh_override') ? BehaviorSettings::OVERRIDE_ALLOW : BehaviorSettings::OVERRIDE_DISALLOW; @@ -344,9 +414,8 @@ class FormManglerService { 'redirect_code' => $form_state->getValue('rh_redirect_code') ?: BehaviorSettings::REDIRECT_NOT_APPLICABLE, ), - $entity->getEntityType()->id(), - $entity->id(), - $form_state->getValue('rh_is_bundle') + $form_state->getValue('rh_entity_type'), + isset($entity) ? $entity->id() : $form_state->getValue('rh_entity_type') ); } } @@ -371,7 +440,7 @@ class FormManglerService { * @param type $form_id The form ID */ protected function populateExtraBehaviorSections(&$form, $form_state, - $form_id, Entity $entity, $entity_is_bundle = FALSE, + $form_id, Entity $entity = NULL, $entity_is_bundle = FALSE, ImmutableConfig $bundle_settings = NULL) { foreach ($this->rhBehaviorPluginManager->getDefinitions() as $id => $def) { diff --git a/src/Plugin/RabbitHoleBehaviorPlugin/PageRedirect.php b/src/Plugin/RabbitHoleBehaviorPlugin/PageRedirect.php index 667c4a8..3de1af6 100644 --- a/src/Plugin/RabbitHoleBehaviorPlugin/PageRedirect.php +++ b/src/Plugin/RabbitHoleBehaviorPlugin/PageRedirect.php @@ -225,9 +225,11 @@ class PageRedirect extends RabbitHoleBehaviorPluginBase { */ public function alterExtraFields(array &$fields) { $fields['rh_redirect'] = BaseFieldDefinition::create('string') + ->setName('rh_redirect') ->setLabel($this->t('Rabbit Hole redirect path or code')) ->setDescription($this->t('The path to where the user should get redirected to.')); $fields['rh_redirect_response'] = BaseFieldDefinition::create('integer') + ->setName('rh_redirect_response') ->setLabel($this->t('Rabbit Hole redirect response code')) ->setDescription($this->t('Specifies the HTTP response code that should be used when perform a redirect.')); } diff --git a/src/Plugin/RabbitHoleEntityPluginBase.php b/src/Plugin/RabbitHoleEntityPluginBase.php new file mode 100644 index 0000000..a633371 --- /dev/null +++ b/src/Plugin/RabbitHoleEntityPluginBase.php @@ -0,0 +1,44 @@ +alterInfo('rabbit_hole_rabbit_hole_entity_plugin_info'); + $this->setCacheBackend($cache_backend, 'rabbit_hole_rabbit_hole_entity_plugin_plugins'); + + $this->etm = $etm; + } + + /** + * Create an instance of the first plugin found supporting the entity type + * with string id $entity_type + * @param string $entity_type + * The string ID of the entity type + * @return Drupal\rabbit_hole\Plugin\RabbitHoleEntityPluginInterface + * The plugin + */ + public function createInstanceByEntityType($entity_type) { + $plugin_ids = array_keys($this->loadDefinitionsByEntityType($entity_type)); + return $this->createInstance($plugin_ids[0]); + } + + /** + * Load plugins implementing entity with id $entity_type + * @param string $entity_type + * The string ID of the entity type + * @return array + * An array of plugin definitions for the entity type with ID $entity_type + */ + public function loadDefinitionsByEntityType($entity_type) { + return array_filter($this->getDefinitions(), function($var) use ($entity_type) { + return $var['entity_type'] == $entity_type; + }); + } + + /** + * Load the string IDs for the supported entity types + * @return array + * An array of entity type ID strings + */ + public function loadSupportedEntityTypes() { + return array_values(array_map(function($var) { + return $var['entity_type']; + }, $this->getDefinitions())); + } + + /** + * Load the string IDs for the supported bundle entity types + * @return array + * An array of entity type ID strings + */ + public function loadSupportedBundleEntityTypes() { + return array_values(array_map(function($var) { + return $this->etm->getStorage($var['entity_type']) + ->getEntityType()->getBundleEntityType(); + }, $this->getDefinitions())); + } + + /** + * Load the string IDs for the global configuration + * forms for entity types + * @return array + * An array of entity types and form ID strings in the form + * form_id => entity_type + */ + public function loadSupportedGlobalForms() { + $result = array(); + foreach ($this->getDefinitions() as $key => $def) { + $form_id = $this->createInstance($key)->getGlobalConfigFormId(); + if (isset($form_id)) { + $result[$form_id] = $def['entity_type']; + } + } + return $result; + } +} diff --git a/src/Tests/RabbitHoleBehaviorSettingsTest.php b/src/Tests/RabbitHoleBehaviorSettingsTest.php index 3ccd155..22d05d8 100644 --- a/src/Tests/RabbitHoleBehaviorSettingsTest.php +++ b/src/Tests/RabbitHoleBehaviorSettingsTest.php @@ -65,7 +65,7 @@ class RabbitHoleBehaviorSettingsTest extends WebTestBase { * Test that the default bundle settings exist and have the expected values */ public function testBundleSettingsDefault() { - $settings = \Drupal::config('rabbit_hole.behavior_settings.default_bundle'); + $settings = \Drupal::config('rabbit_hole.behavior_settings.default'); $this->assertEqual($settings->get('action'), self::DEFAULT_BUNDLE_ACTION, 'Unexpected default action');