MDL-32279 filters: add support for setup() methods in filters.
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Thu, 14 Jun 2012 17:49:13 +0000 (19:49 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Thu, 14 Jun 2012 18:02:20 +0000 (20:02 +0200)
lib/filterlib.php
lib/weblib.php

index 62e68c1..ca75b0d 100644 (file)
@@ -222,6 +222,29 @@ class filter_manager {
         }
         return implode('-', $hashes);
     }
+
+    /**
+     * Setup page with filters requirements and other prepare stuff.
+     *
+     * This method is used by {@see format_text()} and {@see format_string()}
+     * in order to allow filters to setup any page requirement (js, css...)
+     * or perform any action needed to get them prepared before filtering itself
+     * happens by calling to each every active setup() method.
+     *
+     * Note it's executed for each piece of text filtered, so filter implementations
+     * are responsible of controlling the cardinality of the executions that may
+     * be different depending of the stuff to prepare.
+     *
+     * @param moodle_page $page the page we are going to add requirements to.
+     * @param context $context the context which contents are going to be filtered.
+     * @since 2.3
+     */
+    public function setup_page_for_filters($page, $context) {
+        $filters = $this->get_text_filters($context);
+        foreach ($filters as $filter) {
+            $filter->setup($page, $context);
+        }
+    }
 }
 
 /**
@@ -357,6 +380,24 @@ abstract class moodle_text_filter {
         return __CLASS__;
     }
 
+    /**
+     * Setup page with filter requirements and other prepare stuff.
+     *
+     * Override this method if the filter needs to setup page
+     * requirements or needs other stuff to be executed.
+     *
+     * Note this method is invoked from {@see setup_page_for_filters()}
+     * for each piece of text being filtered, so it is responsible
+     * for controlling its own execution cardinality.
+     *
+     * @param moodle_page $page the page we are going to add requirements to.
+     * @param context $context the context which contents are going to be filtered.
+     * @since 2.3
+     */
+    public function setup($page, $context) {
+        // Override me, if needed.
+    }
+
     /**
      * Override this function to actually implement the filtering.
      *
index f68f3d5..d84d16d 100644 (file)
@@ -1090,6 +1090,7 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_
 
     if ($options['filter']) {
         $filtermanager = filter_manager::instance();
+        $filtermanager->setup_page_for_filters($PAGE, $context); // Setup global stuff filters may have.
     } else {
         $filtermanager = new null_filter_manager();
     }
@@ -1302,6 +1303,7 @@ function format_string($string, $striplinks = true, $options = NULL) {
 
     if (!empty($CFG->filterall)) {
         $string = filter_manager::instance()->filter_string($string, $options['context']);
+        $filtermanager->setup_page_for_filters($PAGE, $options['context']); // Setup global stuff filters may have.
     }
 
     // If the site requires it, strip ALL tags from this string