Selaa lähdekoodia

Working admin UI.

Frederic G. MARAND 7 vuotta sitten
vanhempi
sitoutus
16ae316279

+ 2 - 2
README.md

@@ -18,8 +18,8 @@ for authenticated users.
   * Before: `<a href="http://example.com:8080/somepath">Some link</a>`
   * After: `<a href="/somepath">Some link</a>`
 * Image
-  * Before: `<img src="http://dev.example.com/files/image.jpg" alt="Some image" />`
-  * After: `<img src="/files/image.jpg" alt="Some image" />`
+  * Before: `<img src="http://a.example.com/files/img.jpg" alt="Some image" />`
+  * After: `<img src="/files/img.jpg" alt="Some image" />`
 
 You can setup such replacements in the URL Replace Filter settings as follow:
 

+ 22 - 12
src/Plugin/Filter/UrlReplaceFilter.php

@@ -22,7 +22,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  *   title = @Translation("URL Replace filter"),
  *   type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
  *   settings = {
- *     "replacements" = ''
+ *     "replacements" = ""
  *   }
  * )
  */
@@ -31,11 +31,15 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
   const SETTING_NAME = 'replacements';
 
   /**
+   * The current_route_match service.
+   *
    * @var \Drupal\Core\Routing\CurrentRouteMatch
    */
   protected $currentRouteMatch;
 
   /**
+   * The messenger service.
+   *
    * @var \Drupal\Core\Messenger\MessengerInterface
    */
   protected $messenger;
@@ -45,9 +49,9 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
    *
    * @param array $configuration
    *   The plugin configuration.
-   * @param $plugin_id
+   * @param string $plugin_id
    *   The plugin id.
-   * @param $plugin_definition
+   * @param array $plugin_definition
    *   The plugin definition.
    * @param \Drupal\Core\Routing\CurrentRouteMatch $currentRouteMatch
    *   The current_route_match service.
@@ -57,7 +61,7 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
   public function __construct(
     array $configuration,
     $plugin_id,
-    $plugin_definition,
+    array $plugin_definition,
     CurrentRouteMatch $currentRouteMatch,
     MessengerInterface $messenger
   ) {
@@ -159,8 +163,13 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
       return !(empty($setting['original']) && empty($setting['replacement']));
     });
 
-    $result = serialize($validSettings);
-    $form_state->setValue(['filters', 'url_replace_filter', 'settings', self::SETTING_NAME], $result);
+    $result = serialize(array_values($validSettings));
+    $form_state->setValue([
+      'filters',
+      'url_replace_filter',
+      'settings',
+      self::SETTING_NAME,
+    ], $result);
 
     if (empty($validSettings)) {
       $parameterName = 'filter_format';
@@ -170,7 +179,8 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
         '@format' => $parameterValue->label(),
         ':edit' => Url::fromRoute('entity.filter_format.edit_form', [
           $parameterName => $parameterValue->id(),
-      ])->toString()]), MessengerInterface::TYPE_WARNING);
+        ])->toString(),
+      ]), MessengerInterface::TYPE_WARNING);
     }
   }
 
@@ -209,7 +219,7 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
   /**
    * Filter the given text.
    */
-  public function _url_replace_filter_process($text, $format) {
+  public function doProcess($text, $format) {
     $settings = _url_replace_filter_get_settings($format);
     foreach ($settings as $setting) {
       if ($setting['original']) {
@@ -247,8 +257,8 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
    * @return string
    *   A formatted string.
    */
-  public static function processPHPCallback(array $matches) {
-    return self::processPHP($matches[1]);
+  public static function processPhpCallback(array $matches) {
+    return self::processPhp($matches[1]);
   }
 
   /**
@@ -302,7 +312,7 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
   /**
    * Helper function for processCode().
    */
-  public static function processPHPInline($matches) {
+  public static function processPhpInline($matches) {
     // Undo nl2br.
     $text = str_replace('<br />', '', $matches[0]);
     // Decode entities (the highlighter re-entifies) and highlight text.
@@ -315,7 +325,7 @@ class UrlReplaceFilter extends FilterBase implements ContainerFactoryPluginInter
   /**
    * Processes chunks of escaped PHP code into HTML.
    */
-  public static function processPHP($text) {
+  public static function processPhp($text) {
     // Note, pay attention to odd preg_replace-with-/e behaviour on slashes.
     // Undo possible linebreak filter conversion.
     $text = preg_replace('@</?(br|p)\s*/?>@', '', str_replace('\"', '"', $text));

+ 17 - 1
templates/url-replace-filter-settings-form.html.twig

@@ -1 +1,17 @@
-{{ replacements }}
+<table>
+  <thead>
+    <tr>
+      <th>{{ "Original"|trans }}</th>
+      <th>{{ "Replacement"|trans }}</th>
+    </tr>
+  </thead>
+  <tbody>
+    {% for key, child in replacements if key|first != '#' %}
+      <tr>
+        <td>{{ child.original }}</td>
+        <td>{{ child.replacement }}</td>
+      </tr>
+      {{ child|without('original', 'replacement') }}
+    {% endfor %}
+  </tbody>
+</table>

+ 3 - 1
url_replace_filter.info.yml

@@ -3,5 +3,7 @@ type: module
 description: "Allows administrators to replace base URLs in &lt;img&gt; and &lt;a&gt; elements."
 package: OSInet
 core: 8.x
+php: 7.0
+configure: filter.admin_overview
 dependencies:
-  - filter
+  - drupal:filter