You are here

function emptyparagraphkiller_filter in Empty paragraph killer 6

Implementation of hook_filter().

File

./emptyparagraphkiller.module, line 12
Empty paragraph killer: because users are sometimes overzealous with the return key.

Code

function emptyparagraphkiller_filter($op, $delta = 0, $format = -1, $text = '', $cache_id = 0) {
  switch ($op) {
    case 'list':
      return array(
        0 => t('Empty paragraph killer'),
        1 => t('Arbitrary empty HTML tag killer'),
        2 => t('Preceding space destroyer'),
      );
    case 'description':
      switch ($delta) {
        case 0:
          return t('Content editors/creators sometimes hit the carriage return
          twice at the end of a paragraph. This may not be in keeping with the
          look and feel of your site. When entering more than one carriage
          return, this filter will ensure that only the first will be honored.
          If your content is prone to having other empty HTML tags as well as
          the paragraph tag, use the "Arbitrary empty HTML tag killer", and
          choose the tags to be filtered.');
        case 1:
          return t('Allows you to filter out selected html tags which contain
          no content. If your primary concern is empty paragraph tags, use the
          "Empty paragraph killer" for higher performance and zero
          configuration. Also enable and configure the HTML filter when using
          this filter.');
        case 2:
          return t('Removes non-breaking spaces (<strong>&amp;nbsp;</strong>) at
          the beginning of sentences. These are often created unknowingly by
          content editors/creators on text fields with WYSIWYG editors.');
      }
    case 'no cache':

      // This case can be removed for most filters, but returning TRUE is useful for development.
      return TRUE;
    case 'prepare':
      return $text;
    case 'process':
      switch ($delta) {
        case 0:

          // Empty paragraph killer filter
          $text = preg_replace('#<p[^>]*>(\\s|&nbsp;?)*</p>#', '', $text);
          return $text;
        case 1:

          // Filter arbitrary set of tags chosen by the site administrator.
          // Iterate over each configured HTML tag and chack if the tag is empty.
          foreach (explode(' ', variable_get("emptyparagraphkiller_tags_{$format}", 'p')) as $tag) {
            $text = preg_replace('#<' . $tag . '[^>]*>(\\n|\\r\\n|\\s|&nbsp;?)*</' . $tag . '>#', '', $text);
          }

          // If the result only contains whitespaces, reurn an empty string.
          return trim($text) == '' ? '' : $text;
        case 2:

          // Removes &npsp; at the beginning of paragraphs
          $text = preg_replace('#<p[^>]*>(\\s|&nbsp;?)#', '<p>', $text);
          return $text;
      }
    case 'settings':
    case 0:
    case 1:
      $form['emptyparagraphkiller'] = array(
        '#type' => 'fieldset',
        '#title' => t('Empty paragraph killer'),
        '#collapsible' => TRUE,
      );
      $form['emptyparagraphkiller']["emptyparagraphkiller_tags_{$format}"] = array(
        '#type' => 'textfield',
        '#title' => t('HTML tags'),
        '#default_value' => variable_get("emptyparagraphkiller_tags_{$format}", 'p'),
        '#required' => TRUE,
        '#description' => t('Add a list of tag names, separated by a space, to
           check whether or not the element is empty. Empty elements will be
           stripped from the filtered string.'),
      );
      return $form;
    case 2:
  }
}