Skip to content
Snippets Groups Projects

Deleted Text.php file and updated composer.json

Files
10
@@ -2,80 +2,72 @@
namespace Drupal\paragraphs_paste\Plugin\ParagraphsPastePlugin;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\paragraphs_paste\ParagraphsPastePluginBase;
use Netcarver\Textile\Parser;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines the "text" plugin.
* Defines the "custom_text" plugin.
*
* @ParagraphsPastePlugin(
* id = "text",
* label = @Translation("Text"),
* label = @Translation("Text text"),
* module = "paragraphs_paste",
* weight = -1,
* weight = 0,
* allowed_field_types = {"text", "text_long", "text_with_summary", "string",
* "string_long"}
* )
*/
class Text extends ParagraphsPastePluginBase {
/**
* The textile Parser.
*
* @var \Netcarver\Textile\Parser
*/
protected $textileParser;
/**
* {@inheritdoc}
*/
public static function isApplicable($input, array $definition) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->setTextileParser(new Parser());
if (empty(trim($input))) {
return FALSE;
}
return $instance;
}
return !empty(trim(self::cleanHtml($input)));
/**
* Sets the parser for this plugin.
*
* @param \Netcarver\Textile\Parser $parser
* The textile parser.
*/
protected function setTextileParser(Parser $parser) {
$this->textileParser = $parser;
}
/**
* {@inheritdoc}
*/
protected function formatInput($value, FieldDefinitionInterface $fieldDefinition) {
return $this->parseTextileInput($value);
}
if ($fieldDefinition->getType() == 'string') {
$value = html_entity_decode($value);
return trim(preg_replace('/\s+/', ' ', strip_tags($value)));
}
if ($fieldDefinition->getType() == 'string_long') {
$value = html_entity_decode($value);
$lines = array_map('trim', explode(PHP_EOL, strip_tags($value)));
return implode(PHP_EOL, $lines);
}
// For 'text', 'text_long', 'text_with_summary', do:
// Clean newlines and empty paragraphs.
$value = preg_replace('~[\r\n]+|<p[^>]*>([\s]|&nbsp;)*<\/p>~', '', $value);
// Remove trailing whitespace chars and fix html.
$value = rtrim(self::cleanHtml($value));
return $value;
/**
* {@inheritdoc}
*/
public static function isApplicable($input, array $definition) {
return !empty(trim($input)) && class_exists('\Netcarver\Textile\Parser');
}
/**
* Clean html.
*
* @param string $html
* Html string to clean.
*
* @return string
* Cleaned html.
* Use textile to parse input.
*/
protected static function cleanHtml($html) {
$document = Html::load($html);
$xpath = new \DOMXPath($document);
// Remove empty html tags recursively.
while (($node_list = $xpath->query('//*[not(node())]')) && $node_list->length) {
foreach ($node_list as $node) {
$node->parentNode->removeChild($node);
}
}
return Html::serialize($document);
public function parseTextileInput($input) {
$input = preg_replace('~\r?\n~', "\n", $input);
return $this->textileParser->setBlockTags(TRUE)->setRestricted(TRUE)->parse($input);
}
}
Loading