diff --git a/config/install/rabbit_hole.behavior_settings.default.yml b/config/install/rabbit_hole.behavior_settings.default.yml index 446c8b4..016504a 100644 --- a/config/install/rabbit_hole.behavior_settings.default.yml +++ b/config/install/rabbit_hole.behavior_settings.default.yml @@ -4,4 +4,4 @@ action: "display_page" allow_override: 1 # N/A redirect_code: 0 -redirect_path: '' +redirect: '' diff --git a/config/install/rabbit_hole.behavior_settings.default_bundle.yml b/config/install/rabbit_hole.behavior_settings.default_bundle.yml index 0c4bf0b..5504cb3 100644 --- a/config/install/rabbit_hole.behavior_settings.default_bundle.yml +++ b/config/install/rabbit_hole.behavior_settings.default_bundle.yml @@ -4,4 +4,4 @@ action: "display_page" allow_override: 1 # N/A redirect_code: 0 -redirect_path: '' +redirect: '' diff --git a/config/schema/behavior_settings.schema.yml b/config/schema/behavior_settings.schema.yml index 340ca53..80fbd17 100644 --- a/config/schema/behavior_settings.schema.yml +++ b/config/schema/behavior_settings.schema.yml @@ -13,7 +13,7 @@ rabbit_hole.behavior_settings.*: allow_override: type: integer label: 'Allow override' - redirect_path: + redirect: type: string label: 'Redirect path' redirect_code: diff --git a/src/BehaviorSettingsInterface.php b/src/BehaviorSettingsInterface.php index 3c2cd18..d2599c4 100644 --- a/src/BehaviorSettingsInterface.php +++ b/src/BehaviorSettingsInterface.php @@ -63,10 +63,10 @@ interface BehaviorSettingsInterface extends ConfigEntityInterface { /** * Set the redirect path if action is redirect. * - * @param string $redirect_path + * @param string $redirect * The redirect path. */ - public function setRedirectPath($redirect_path); + public function setRedirectPath($redirect); /** * Get the redirect path if action is redirect. diff --git a/src/Entity/BehaviorSettings.php b/src/Entity/BehaviorSettings.php index f3b3382..715c796 100644 --- a/src/Entity/BehaviorSettings.php +++ b/src/Entity/BehaviorSettings.php @@ -25,7 +25,7 @@ use Drupal\rabbit_hole\Exception\InvalidBehaviorSettingException; * "uuid" = "uuid", * "action" = "action", * "allow_override" = "allow_override", - * "redirect_path" = "redirect_path", + * "redirect" = "redirect", * "redirect_code" = "redirect_code" * }, * links = {} @@ -68,7 +68,7 @@ class BehaviorSettings extends ConfigEntityBase implements BehaviorSettingsInter * @todo It may be possible to make this reliant on a plugin instead (i.e. * the redirect plugin) - if so, we should probably do this */ - protected $redirect_path; + protected $redirect; /** * The code to use for redirects (if the action is redirect). @@ -145,18 +145,18 @@ class BehaviorSettings extends ConfigEntityBase implements BehaviorSettingsInter /** * {@inheritdoc} */ - public function setRedirectPath($redirect_path) { - if ($this->action !== 'redirect' && $redirect_path != "") { - throw new InvalidBehaviorSettingException('redirect_path'); + public function setRedirectPath($redirect) { + if ($this->action !== 'redirect' && $redirect != "") { + throw new InvalidBehaviorSettingException('redirect'); } - $this->redirect_path = $redirect_path; + $this->redirect = $redirect; } /** * {@inheritdoc} */ public function getRedirectPath() { - return $this->redirect_path; + return $this->redirect; } } diff --git a/src/FormManglerService.php b/src/FormManglerService.php index 0ccc6da..cc986cb 100644 --- a/src/FormManglerService.php +++ b/src/FormManglerService.php @@ -295,9 +295,9 @@ class FormManglerService { array( 'action' => $form_state->getValue('rh_action'), 'allow_override' => $allow_override, - 'redirect_path' => $form_state->getValue('rh_redirect_path') + 'redirect' => $form_state->getValue('rh_redirect') ?: '', - 'redirect_code' => $form_state->getValue('rh_redirect_code') + 'redirect_code' => $form_state->getValue('rh_redirect_response') ?: BehaviorSettings::REDIRECT_NOT_APPLICABLE, ), $form_state->getValue('rh_entity_type'), diff --git a/src/Plugin/RabbitHoleBehaviorPlugin/PageRedirect.php b/src/Plugin/RabbitHoleBehaviorPlugin/PageRedirect.php index 57fd635..1f0b73d 100644 --- a/src/Plugin/RabbitHoleBehaviorPlugin/PageRedirect.php +++ b/src/Plugin/RabbitHoleBehaviorPlugin/PageRedirect.php @@ -12,10 +12,13 @@ use Drupal\Core\Link; use Drupal\Core\Entity\Entity; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Config\ImmutableConfig; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\rabbit_hole\Plugin\RabbitHoleBehaviorPluginBase; use Drupal\rabbit_hole\Exception\InvalidRedirectResponseException; +use Drupal\rabbit_hole\BehaviorSettingsManagerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Redirects to another page. @@ -25,7 +28,7 @@ use Symfony\Component\HttpFoundation\Response; * label = @Translation("Page redirect") * ) */ -class PageRedirect extends RabbitHoleBehaviorPluginBase { +class PageRedirect extends RabbitHoleBehaviorPluginBase implements ContainerFactoryPluginInterface { use StringTranslationTrait; const RABBIT_HOLE_PAGE_REDIRECT_DEFAULT = ''; @@ -53,10 +56,31 @@ class PageRedirect extends RabbitHoleBehaviorPluginBase { private $code; /** + * The behavior settings manager + * + * Drupal\rabbit_hole\BehaviorSettingsManagerInterface + */ + protected $rhBehaviorSettingsManager; + + /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, + BehaviorSettingsManagerInterface $bsm) { parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->rhBehaviorSettingsManager = $bsm; + } + + /** + *{@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static ( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('rabbit_hole.behavior_settings_manager') + ); } /** @@ -65,18 +89,32 @@ class PageRedirect extends RabbitHoleBehaviorPluginBase { public function performAction(Entity $entity, Response $current_response = NULL) { // Return new RedirectResponse($this->path, $this->code);. $target = $entity->get('rh_redirect')->value; + $response_code = NULL; + + $bundle_entity_type = $entity->getEntityType()->getBundleEntityType(); + $bundle_settings = $this->rhBehaviorSettingsManager + ->loadBehaviorSettingsAsConfig($bundle_entity_type, + $entity->bundle()); + + if (!isset($target)) { + $target = $bundle_settings->get('redirect'); + $response_code = $bundle_settings->get('redirect_code'); + } + else { + $response_code = $entity->get('rh_redirect_response')->value; + } + if (substr($target, 0, 4) == 'get('rh_redirect_response')->value) { + switch ($response_code) { case self::REDIRECT_MOVED_PERMANENTLY: case self::REDIRECT_FOUND: case self::REDIRECT_SEE_OTHER: case self::REDIRECT_TEMPORARY_REDIRECT: if ($current_response === NULL) { - return new RedirectResponse($target, - $entity->get('rh_redirect_response')->value); + return new RedirectResponse($target, $response_code); } else { // If a response already exists we don't need to do anything with it. @@ -118,16 +156,16 @@ class PageRedirect extends RabbitHoleBehaviorPluginBase { public function settingsForm(&$form, &$form_state, $form_id, Entity $entity = NULL, $entity_is_bundle = FALSE, ImmutableConfig $bundle_settings = NULL) { - $redirect_path = NULL; + $redirect = NULL; $redirect_code = NULL; if (isset($entity)) { if ($entity_is_bundle) { - $redirect_path = $bundle_settings->get('redirect_path'); + $redirect = $bundle_settings->get('redirect'); $redirect_code = $bundle_settings->get('redirect_code'); } else { - $redirect_path = isset($entity->rh_redirect->value) + $redirect = isset($entity->rh_redirect->value) ? $entity->rh_redirect->value : self::RABBIT_HOLE_PAGE_REDIRECT_DEFAULT; $redirect_code = isset($entity->rh_redirect_code->value) @@ -136,7 +174,7 @@ class PageRedirect extends RabbitHoleBehaviorPluginBase { } } else { - $redirect_path = NULL; + $redirect = NULL; $redirect_code = NULL; } @@ -161,10 +199,10 @@ class PageRedirect extends RabbitHoleBehaviorPluginBase { $form['rabbit_hole']['redirect']['rh_redirect'] = array( '#type' => /*rabbit_hole_access_php($module) ? 'textarea' :*/ 'textfield', '#title' => t('Redirect path'), - '#default_value' => $redirect_path, + '#default_value' => $redirect, '#description' => '

' . implode('

', $description) . '

', '#attributes' => array('class' => array('rabbit-hole-redirect-setting')), - '#rows' => substr_count($redirect_path, "\r\n") + 2, + '#rows' => substr_count($redirect, "\r\n") + 2, ); // Display a list of tokens if the Token module is enabled. // Add the redirect response setting. diff --git a/src/Tests/RabbitHoleBehaviorSettingsEntityMethodsTest.php b/src/Tests/RabbitHoleBehaviorSettingsEntityMethodsTest.php index 0d3e418..01e4830 100644 --- a/src/Tests/RabbitHoleBehaviorSettingsEntityMethodsTest.php +++ b/src/Tests/RabbitHoleBehaviorSettingsEntityMethodsTest.php @@ -46,7 +46,7 @@ class RabbitHoleBehaviorSettingsEntityMethodsTest extends WebTestBase { public function testCreateEntity() { $action = 'page_not_found'; $redirect_code = BehaviorSettings::REDIRECT_NOT_APPLICABLE; - $redirect_path = '/'; + $redirect = '/'; $allow_override = BehaviorSettings::OVERRIDE_ALLOW; $entity = BehaviorSettings::create( @@ -55,7 +55,7 @@ class RabbitHoleBehaviorSettingsEntityMethodsTest extends WebTestBase { 'action' => $action, 'allow_override' => $allow_override, 'redirect_code' => $redirect_code, - 'redirect_path' => $redirect_path, + 'redirect' => $redirect, ) ); $entity->save(); @@ -64,7 +64,7 @@ class RabbitHoleBehaviorSettingsEntityMethodsTest extends WebTestBase { $this->assertEqual($action, $config_entity->get('action')); $this->assertEqual($redirect_code, $config_entity->get('redirect_code')); - $this->assertEqual($redirect_path, $config_entity->get('redirect_path')); + $this->assertEqual($redirect, $config_entity->get('redirect')); $this->assertEqual($allow_override, $config_entity->get('allow_override')); } @@ -150,7 +150,7 @@ class RabbitHoleBehaviorSettingsEntityMethodsTest extends WebTestBase { 'id' => 'test_behavior_settings', 'action' => 'access_denied', 'redirect_code' => BehaviorSettings::REDIRECT_NOT_APPLICABLE, - 'redirect_path' => NULL, + 'redirect' => NULL, ) ); } diff --git a/src/Tests/RabbitHoleBehaviorSettingsTest.php b/src/Tests/RabbitHoleBehaviorSettingsTest.php index b7b0e53..42dd0a8 100644 --- a/src/Tests/RabbitHoleBehaviorSettingsTest.php +++ b/src/Tests/RabbitHoleBehaviorSettingsTest.php @@ -129,7 +129,7 @@ class RabbitHoleBehaviorSettingsTest extends WebTestBase { 'action' => $expected_action, 'allow_override' => 0, 'redirect_code' => 0, - 'redirect_path' => '', + 'redirect' => '', ), $entity_type_label, $entity_id); $action = $this->behaviorSettingsManager->loadBehaviorSettingsAsConfig( $entity_type_label, $entity_id)->get('action');