mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
Add a plain_text Quantity view mode that strips tags and whitespace.
This commit is contained in:
parent
ee67c24af1
commit
0b361341e3
|
@ -0,0 +1,12 @@
|
|||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
enforced:
|
||||
module:
|
||||
- quantity
|
||||
module:
|
||||
- quantity
|
||||
id: quantity.plain_text
|
||||
label: Plain text
|
||||
targetEntityType: quantity
|
||||
cache: false
|
31
modules/core/quantity/quantity.post_update.php
Normal file
31
modules/core/quantity/quantity.post_update.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Post update hooks for the quantity module.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Entity\Entity\EntityViewMode;
|
||||
|
||||
/**
|
||||
* Create plain text view mode for quantities.
|
||||
*/
|
||||
function quantity_post_update_plain_text_view_mode(&$sandbox) {
|
||||
$view_mode = EntityViewMode::create([
|
||||
'id' => 'quantity.plain_text',
|
||||
'label' => 'Plain text',
|
||||
'targetEntityType' => 'quantity',
|
||||
'cache' => FALSE,
|
||||
'dependencies' => [
|
||||
'enforced' => [
|
||||
'module' => [
|
||||
'quantity',
|
||||
],
|
||||
],
|
||||
'module' => [
|
||||
'quantity',
|
||||
],
|
||||
],
|
||||
]);
|
||||
$view_mode->save();
|
||||
}
|
|
@ -30,7 +30,7 @@ use Drupal\user\EntityOwnerTrait;
|
|||
* "inline_form" = "\Drupal\quantity\Form\QuantityInlineForm",
|
||||
* "list_builder" = "\Drupal\quantity\QuantityListBuilder",
|
||||
* "permission_provider" = "\Drupal\entity\UncacheableEntityPermissionProvider",
|
||||
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
|
||||
* "view_builder" = "Drupal\quantity\QuantityViewBuilder",
|
||||
* "views_data" = "Drupal\quantity\QuantityViewsData",
|
||||
* "form" = {
|
||||
* "default" = "Drupal\Core\Entity\ContentEntityForm",
|
||||
|
|
54
modules/core/quantity/src/QuantityViewBuilder.php
Normal file
54
modules/core/quantity/src/QuantityViewBuilder.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\quantity;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Entity\EntityViewBuilder;
|
||||
use Drupal\Core\Security\TrustedCallbackInterface;
|
||||
|
||||
/**
|
||||
* Quantity entity view builder.
|
||||
*/
|
||||
class QuantityViewBuilder extends EntityViewBuilder implements TrustedCallbackInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
|
||||
$build = parent::view($entity, $view_mode, $langcode);
|
||||
|
||||
// If the view mode is plain_text, add a post render callback to strip tags
|
||||
// and whitespace.
|
||||
if ($view_mode == 'plain_text') {
|
||||
$build['#post_render'][] = [$this, 'plainText'];
|
||||
}
|
||||
|
||||
return $build;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function trustedCallbacks() {
|
||||
return array_merge(parent::trustedCallbacks(), ['plainText']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Strips HTML, newlines, and whitespace from rendered entity.
|
||||
*
|
||||
* @param string $value
|
||||
* A string of rendered content for the Quantity entity.
|
||||
*
|
||||
* @return string
|
||||
* The updated content.
|
||||
*/
|
||||
public static function plainText(string $value) {
|
||||
$value = Html::decodeEntities($value);
|
||||
$value = strip_tags($value);
|
||||
$value = trim($value);
|
||||
$value = preg_replace('/\s+/', ' ', $value);
|
||||
return $value;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue