You are here

class DiffFormatter in Entity Share 8.3

Diff formatter which renders a table, with structured padding in HTML.

Hierarchy

Expanded class hierarchy of DiffFormatter

1 string reference to 'DiffFormatter'
entity_share_diff.services.yml in modules/entity_share_diff/entity_share_diff.services.yml
modules/entity_share_diff/entity_share_diff.services.yml
1 service uses DiffFormatter
entity_share_diff.formatter in modules/entity_share_diff/entity_share_diff.services.yml
Drupal\entity_share_diff\Diff\DiffFormatter

File

modules/entity_share_diff/src/Diff/DiffFormatter.php, line 13

Namespace

Drupal\entity_share_diff\Diff
View source
class DiffFormatter extends CoreDiffFormatter {

  /**
   * Is the Diff supposed to be output in an HTML page?
   *
   * @var bool
   */
  public $htmlOutput = FALSE;

  /**
   * Creates a DiffFormatter to render diffs in a table.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    parent::__construct($config_factory);
    $config = $config_factory
      ->get('entity_share_diff.settings');
    $context_settings = $config
      ->get('context');
    if ($context_settings) {
      $this->leading_context_lines = $context_settings['lines_leading'];
      $this->leading_context_lines = $context_settings['lines_trailing'];
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function addedLine($line) {
    $output = parent::addedLine($line);
    return $this
      ->improvePadding($output);
  }

  /**
   * {@inheritdoc}
   */
  protected function deletedLine($line) {
    $output = parent::deletedLine($line);
    return $this
      ->improvePadding($output);
  }

  /**
   * {@inheritdoc}
   */
  protected function contextLine($line) {
    $output = parent::contextLine($line);
    return $this
      ->improvePadding($output);
  }

  /**
   * Helper function: replaces initial plain spaces with HTML spaces in markup.
   *
   * @param array $output
   *   An array representing a table row.
   *
   * @return array
   *   An array representing a table row.
   */
  protected function improvePadding(array $output) {
    if (!$this->htmlOutput) {
      return $output;
    }
    $markup = $output[1]['data']['#markup'];
    $trimmed_markup = ltrim($markup);
    $diff_length = strlen($markup) - strlen($trimmed_markup);
    if ($diff_length > 0) {
      $output[1]['data']['#markup'] = str_repeat(' ', $diff_length) . $trimmed_markup;
    }
    return $output;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DiffFormatter::$htmlOutput public property Is the Diff supposed to be output in an HTML page?
DiffFormatter::$leading_context_lines public property Number of leading context "lines" to preserve.
DiffFormatter::$line_stats protected property The line stats.
DiffFormatter::$rows protected property The diff represented as an array of rows.
DiffFormatter::$show_header public property Should a block header be shown?
DiffFormatter::$trailing_context_lines public property Number of trailing context "lines" to preserve.
DiffFormatter::addedLine protected function Creates an added line. Overrides DiffFormatter::addedLine
DiffFormatter::contextLine protected function Creates a context line. Overrides DiffFormatter::contextLine
DiffFormatter::deletedLine protected function Creates a deleted line. Overrides DiffFormatter::deletedLine
DiffFormatter::emptyLine protected function Creates an empty line.
DiffFormatter::format public function Format a diff.
DiffFormatter::improvePadding protected function Helper function: replaces initial plain spaces with HTML spaces in markup.
DiffFormatter::_added protected function Overrides DiffFormatter::_added
DiffFormatter::_block protected function
DiffFormatter::_block_header protected function Overrides DiffFormatter::_block_header
DiffFormatter::_changed protected function Overrides DiffFormatter::_changed
DiffFormatter::_context protected function Overrides DiffFormatter::_context
DiffFormatter::_deleted protected function Overrides DiffFormatter::_deleted
DiffFormatter::_end_block protected function
DiffFormatter::_end_diff protected function Overrides DiffFormatter::_end_diff
DiffFormatter::_lines protected function Overrides DiffFormatter::_lines
DiffFormatter::_start_block protected function Overrides DiffFormatter::_start_block
DiffFormatter::_start_diff protected function Overrides DiffFormatter::_start_diff
DiffFormatter::__construct public function Creates a DiffFormatter to render diffs in a table. Overrides DiffFormatter::__construct