Skip to content

Instantly share code, notes, and snippets.

@sillygwailo
Created October 23, 2013 20:57
Show Gist options
  • Save sillygwailo/7126627 to your computer and use it in GitHub Desktop.
Save sillygwailo/7126627 to your computer and use it in GitHub Desktop.
Instructions on how to update a Drupal 7 filter module to Drupal 8 (to go in a Drupal.org handbook page)
<ol>
<li>create a file called <code>filter.services.yml</code> file, with the following format:
<code><pre>
services:
plugin.manager.filter:
class: Drupal\modulename\Filter\FilterName
arguments: ['@container.namespaces']
</pre></code>
Replace <code>modulename</code> with the module's machine name, and <code>FilterName</code> with a unique name for the filter.</li>
<li>create a folder called <code>lib/Drupal/modulename/Plugin</code> (replacing <code>modulename</code> with the module's machine name). In Mac and Unix environments, in the module's folder, you can use the following shell command:
<code>mkdir -p lib/Drupal/modulename/Plugin</code>
</li>
<li>in the <code>lib/Drupal/modulename/Plugin</code> folder, create a <code>FilterName.php</code> file, replcacing <code>FilterName</code> with the unique name you chose in step 1.</li>
<li>Start the file with the following code (replace <code>FilterName</code> with the unique name):
<code><pre>
/**
* @file
* Contains \Drupal\pirate\Plugin\Filter\FilterName.
*/
namespace Drupal\pirate\Plugin\Filter;
use Drupal\filter\Annotation\Filter;
use Drupal\Core\Annotation\Translation;
use Drupal\filter\Plugin\FilterBase;
</pre></code>
Use annotations for the filter's information and default settings. Here's the example in the Pirate module. This goes directly above the class declaration for the filter:
<code><pre>
/**
* Provides a filter to limit allowed HTML tags.
*
* @Filter(
* id = "filter_pirate",
* module = "pirate",
* title = @Translation("Ah, Squiddy! I got nothing against ye. I just heard there was gold in yer belly. Ha ha har, ha ha ha har!"),
* type = FILTER_TYPE_TRANSFORM_IRREVERSIBLE,
* settings = {
* "pirate_display_tip" = 0,
* },
* weight = -10
* )
*/
</pre></code>
Notice the format of the annotation, especially the settings property.
</li>
<li>Declare your class.
<code><pre>
class FilterPirate extends FilterBase {
...
}
</pre></code>
Inside the class, declare and ... the following methods:
<code><pre>
public function settingsForm(array $form, array &$form_state) {
...
}
</pre></code>
This returns a form using the Forms API.
<code><pre>
/**
* {@inheritdoc}
*/
public function process($text, $langcode, $cache, $cache_id) {
...
}
</pre></code>
You might remember this as the "process callback" property in <code>hook_filter_info()</code> from such versions of Drupal as 7 or the process operation in such versions of Drupal as 4.6, 4.7, 5 and 6.
<code><pre>
/**
* {@inheritdoc}
*/
public function tips($long = FALSE) {
...
}
</pre></code>
You might remember this as <code>hook_filter_tips()</code> from Drupal 6 and before and the "tips callback" property in <code>hook_filter_info</code> in Drupal 7.</li>
</ol>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment