]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - plugins/demo_plugin/demo_plugin.php
Merge pull request #1698 from ArthurHoaro/feature/plugins-search-filter
[github/shaarli/Shaarli.git] / plugins / demo_plugin / demo_plugin.php
index 22d27b6827f306ad3a3230ffd8361cd543a5bd6b..d89765cfcb2ca20bfbf34a4cda8a592669b73524 100644 (file)
@@ -7,6 +7,8 @@
  * Can be used by plugin developers to make their own plugin.
  */
 
+require_once __DIR__ . '/DemoPluginController.php';
+
 /*
  * RENDER HEADER, INCLUDES, FOOTER
  *
@@ -15,6 +17,7 @@
  * and check user status with _LOGGEDIN_.
  */
 
+use Shaarli\Bookmark\Bookmark;
 use Shaarli\Config\ConfigManager;
 use Shaarli\Plugin\PluginManager;
 use Shaarli\Render\TemplatePage;
@@ -60,6 +63,17 @@ function demo_plugin_init($conf)
     return $errors;
 }
 
+function demo_plugin_register_routes(): array
+{
+    return [
+        [
+            'method' => 'GET',
+            'route' => '/custom',
+            'callable' => 'Shaarli\DemoPlugin\DemoPluginController:index',
+        ],
+    ];
+}
+
 /**
  * Hook render_header.
  * Executed on every page render.
@@ -250,6 +264,17 @@ function hook_demo_plugin_render_linklist($data)
     }
     $data['action_plugin'][] = $action;
 
+    // Action to trigger custom filter hiding bookmarks not containing 'e' letter in their description
+    $action = [
+        'attr' => [
+            'href' => '?e',
+            'title' => 'Hide bookmarks without "e" in their description.',
+        ],
+        'html' => 'e',
+        'on' => isset($_GET['e'])
+    ];
+    $data['action_plugin'][] = $action;
+
     // link_plugin (for each link)
     foreach ($data['links'] as &$value) {
         $value['link_plugin'][] = ' DEMO \o/';
@@ -304,7 +329,11 @@ function hook_demo_plugin_render_editlink($data)
 function hook_demo_plugin_render_tools($data)
 {
     // field_plugin
-    $data['tools_plugin'][] = 'tools_plugin';
+    $data['tools_plugin'][] = '<div class="tools-item">
+        <a href="' . $data['_BASE_PATH_'] . '/plugin/demo_plugin/custom">
+          <span class="pure-button pure-u-lg-2-3 pure-u-3-4">Demo Plugin Custom Route</span>
+        </a>
+      </div>';
 
     return $data;
 }
@@ -469,6 +498,27 @@ function hook_demo_plugin_save_plugin_parameters($data)
     return $data;
 }
 
+/**
+ * This hook is called when a search is performed, on every search entry.
+ * It allows to add custom filters, and filter out additional link.
+ *
+ * For exemple here, we hide all bookmarks not containing the letter 'e' in their description.
+ *
+ * @param Bookmark $bookmark Search entry. Note that this is a Bookmark object, and not a link array.
+ *                           It should NOT be altered.
+ * @param array    $context  Additional info on the search performed.
+ *
+ * @return bool True if the bookmark should be kept in the search result, false to discard it.
+ */
+function hook_demo_plugin_filter_search_entry(Bookmark $bookmark, array $context): bool
+{
+    if (isset($_GET['e'])) {
+        return strpos($bookmark->getDescription(), 'e') !== false;
+    }
+
+    return true;
+}
+
 /**
  * This function is never called, but contains translation calls for GNU gettext extraction.
  */