Unverified Commit d893bd85 authored by Jess Straatmann's avatar Jess Straatmann Committed by Andrew Berry
Browse files

Issue #2631500 by jastraat: Drupal 8 Initial port

parent aa854889
Loading
Loading
Loading
Loading
+74 −0
Changes for src/Plugin/Filter/TableAltrow.php: 74 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php
/**
 * @file
 * Contains \Drupal\table_altrow\Plugin\Filter\TableAltrow.
 */

namespace Drupal\table_altrow\Plugin\Filter;

use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;

/**
 * Insert even and odd classes for tables via input filters to allow for proper
 * zebra-style striping.
 *
 * @Filter(
 *   id = "table_altrow",
 *   title = @Translation("Add even and odd classes to table rows."),
 *   type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE,
 * )
 */
class TableAltrow extends FilterBase {

  /**
   * {@inheritdoc}
   */
  public function process($text, $langcode) {
    // The actual filtering is performed here. The supplied text should be
    // returned, once any necessary substitutions have taken place.
    // First, we have to parse the variable.
    $matches = array();
    $offset = 0;
    // Find a tbody.
    while (preg_match('!(<tbody ?.*>)!i', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
      $offset = $matches[0][1];
      $count = 1;
      // While the tbody is still open.
      while (preg_match('!(<tr( ?.*)>)|(</tbody>)!i', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
        // +1 so we don't match the same string.
        $offset = $matches[0][1] + 1;

        // Don't process tr's until we find a tbody.
        if ($matches[0][0] == '</tbody>') {
          break;
        }

        // Don't replace existing classes. Perhaps this should append a class instead?
        if (!strstr($matches[2][0], 'class=')) {
          if (($count % 2) == 0) {
            $new_tag = '<tr class="even"' . $matches[2][0] . '>';
            $text = table_altrow_str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
          }
          else {
            $new_tag = '<tr class="odd"' . $matches[2][0] . '>';
            $text = table_altrow_str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
          }
        }
        $count++;
      }
    }

    return new FilterProcessResult($text);
  }

  /**
   * {@inheritdoc}
   */
  public function tips($long = FALSE) {
    if ($long) {
      return $this->t('Tables will be rendered with different styles for even and odd rows if supported.');
    }
  }

}
 No newline at end of file

table_altrow.info

deleted100644 → 0
+0 −7
Changes for table_altrow.info: 0 added lines, 7 removed lines.
Original line number Diff line number Diff line
name = Table Alternate Rows
description = Automatically adds even/odd classes to tables.
core = 7.x

files[] = table_altrow.module
project = "table_altrow"

table_altrow.info.yml

0 → 100644
+5 −0
Changes for table_altrow.info.yml: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
name: 'Table Alternate Rows'
description: 'Automatically adds even/odd classes to tables.'
core: 8.x
type: module
package: User interface
+0 −62
Changes for table_altrow.module: 0 added lines, 62 removed lines.
Original line number Diff line number Diff line
@@ -6,68 +6,6 @@
 * zebra-style striping.
 */

/**
 * Implements hook_filter_info().
 */
function table_altrow_filter_info() {
  $filters = array();
  $filters['table_altrow'] = array(
    'title' => t('Add even and odd classes to table rows'),
    'process callback' => '_table_altrow_process',
    'tips callback' => 'table_altrow_tips',
  );
  return $filters;
}

/**
 * Filter process callback.
 */
function _table_altrow_process($text, $filter) {
  // The actual filtering is performed here. The supplied text should be
  // returned, once any necessary substitutions have taken place.
  // First, we have to parse the variable.
  $matches = array();
  $offset = 0;
  // Find a tbody.
  while (preg_match('!(<tbody ?.*>)!i', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
    $offset = $matches[0][1];
    $count = 1;
    // While the tbody is still open.
    while (preg_match('!(<tr( ?.*)>)|(</tbody>)!i', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
      // +1 so we don't match the same string.
      $offset = $matches[0][1] + 1;

      // Don't process tr's until we find a tbody.
      if ($matches[0][0] == '</tbody>') {
        break;
      }

      // Don't replace existing classes. Perhaps this should append a class instead?
      if (!strstr($matches[2][0], 'class=')) {
        if (($count % 2) == 0) {
          $new_tag = '<tr class="even"' . $matches[2][0] . '>';
          $text = table_altrow_str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
        }
        else {
          $new_tag = '<tr class="odd"' . $matches[2][0] . '>';
          $text = table_altrow_str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
        }
      }
      $count++;
    }
  }
  return $text;
}

/**
 * Filter tips callback.
 */
function _table_altrow_tips($filter, $format, $long = FALSE) {
  if ($long) {
    return t('Tables will be rendered with different styles for even and odd rows if supported.');
  }
}

/**
 * Replace every instance of a string with a count parameter like PHP5.
 * This can probably be removed with Drupal goes to PHP5 only.