]> 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 15cfc2c51cd4889cc0d38f821e0ab856806a4167..d89765cfcb2ca20bfbf34a4cda8a592669b73524 100644 (file)
@@ -17,6 +17,7 @@ require_once __DIR__ . '/DemoPluginController.php';
  * and check user status with _LOGGEDIN_.
  */
 
+use Shaarli\Bookmark\Bookmark;
 use Shaarli\Config\ConfigManager;
 use Shaarli\Plugin\PluginManager;
 use Shaarli\Render\TemplatePage;
@@ -263,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/';
@@ -486,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.
  */