|
@@ -8,6 +8,7 @@ use Drupal\Core\Messenger\MessengerInterface;
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
|
|
use Drupal\Core\Routing\CurrentRouteMatch;
|
|
|
use Drupal\Core\Url;
|
|
|
+use Drupal\filter\Entity\FilterFormat;
|
|
|
use Drupal\filter\FilterProcessResult;
|
|
|
use Drupal\filter\Plugin\FilterBase;
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
@@ -28,6 +29,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
*/
|
|
|
class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInterface {
|
|
|
|
|
|
+ const ID = 'url_replace_filter';
|
|
|
const SETTING_NAME = 'replacements';
|
|
|
|
|
|
/**
|
|
@@ -54,16 +56,17 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
|
|
|
* @param array $plugin_definition
|
|
|
* The plugin definition.
|
|
|
* @param \Drupal\Core\Routing\CurrentRouteMatch $currentRouteMatch
|
|
|
- * The current_route_match service.
|
|
|
+ * Optional: the current_route_match service. Only used by the settings
|
|
|
+ * form.
|
|
|
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
|
|
|
- * The messenger service.
|
|
|
+ * Optional: the messenger service. Only used by the settings form.
|
|
|
*/
|
|
|
public function __construct(
|
|
|
array $configuration,
|
|
|
$plugin_id,
|
|
|
array $plugin_definition,
|
|
|
- CurrentRouteMatch $currentRouteMatch,
|
|
|
- MessengerInterface $messenger
|
|
|
+ CurrentRouteMatch $currentRouteMatch = NULL,
|
|
|
+ MessengerInterface $messenger = NULL
|
|
|
) {
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
|
|
$this->currentRouteMatch = $currentRouteMatch;
|
|
@@ -114,6 +117,45 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
|
|
|
return $form;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Return the list of input formats containing the active URL Replace filter.
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ * The list of input format names, keyed by format_id.
|
|
|
+ */
|
|
|
+ public static function getFormats() {
|
|
|
+ $formats = FilterFormat::loadMultiple();
|
|
|
+
|
|
|
+ $ret = [];
|
|
|
+ /** @var \Drupal\filter\FilterFormatInterface $format */
|
|
|
+ foreach ($formats as $format_id => $format) {
|
|
|
+ /** @var \Drupal\filter\FilterPluginCollection $filterCollection */
|
|
|
+ $filterCollection = $format->filters();
|
|
|
+ if (!$filterCollection->has(static::ID)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** @var \Drupal\filter\Plugin\FilterInterface $filter */
|
|
|
+ $filter = $filterCollection->get(static::ID);
|
|
|
+ if ($filter->getConfiguration()['status'] ?? FALSE) {
|
|
|
+ $ret[$format_id] = $format->label();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Implements hook_help().
|
|
|
+ */
|
|
|
+ public static function help(string $routeName) {
|
|
|
+ if ($routeName !== 'filter.admin_overview') {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ return t('To configure url_replace_filter, enable its checkbox in each text format where you want it, then configure its rewriting rules in the vertical tab which will appear at the bottom of the format configuration page. The module does not contain any global configuration.');
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* {@inheritdoc}
|
|
|
*/
|
|
@@ -122,6 +164,22 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
|
|
|
$empty = 0;
|
|
|
$form[self::SETTING_NAME] = [
|
|
|
'#type' => 'details',
|
|
|
+ '#description' => $this->t("
|
|
|
+<p>Enter original base URLs and their replacements. Matching not case-sensitive.
|
|
|
+ You may use <em>%baseurl</em> in the replacement string to insert your site's
|
|
|
+ base URL (without the trailing slash).</p>
|
|
|
+
|
|
|
+<p><strong>Warning</strong>: To avoid unexpected results, you must include
|
|
|
+ trailing slashes in both the original and replacement strings.</p>
|
|
|
+
|
|
|
+<p><strong>Warning</strong>: Replacements are executed in the order you give
|
|
|
+ them. Place the most specific URLs first. For example,
|
|
|
+ <em>http://example.com/somepath/</em> should be replaced before
|
|
|
+ <em>http://example.com/</em>.</p>
|
|
|
+
|
|
|
+<p>If you need more replacement rules, more fields will be added after saving
|
|
|
+ the settings.</p>"),
|
|
|
+
|
|
|
'#title' => $this->t('URL Replace Filter'),
|
|
|
'#open' => TRUE,
|
|
|
'#theme' => 'url_replace_filter_settings_form',
|
|
@@ -150,14 +208,14 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Submit handler for _url_replace_filter_settings() form.
|
|
|
+ * Element_validate handler for settingsForm().
|
|
|
*
|
|
|
* Remove useless empty settings to keep variable as small as possible.
|
|
|
*
|
|
|
* Needs to be public to be usable as a #element_validate callback.
|
|
|
*/
|
|
|
public function settingsFormValidate(array $form, FormStateInterface &$form_state) {
|
|
|
- $settings = $form_state->getValue('filters')['url_replace_filter']['settings'][self::SETTING_NAME];
|
|
|
+ $settings = $form_state->getValue('filters')[self::ID]['settings'][self::SETTING_NAME];
|
|
|
|
|
|
$validSettings = array_filter($settings, function (array $setting) {
|
|
|
return !(empty($setting['original']) && empty($setting['replacement']));
|
|
@@ -166,7 +224,7 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
|
|
|
$result = serialize(array_values($validSettings));
|
|
|
$form_state->setValue([
|
|
|
'filters',
|
|
|
- 'url_replace_filter',
|
|
|
+ self::ID,
|
|
|
'settings',
|
|
|
self::SETTING_NAME,
|
|
|
], $result);
|