Entity.php in Convert Media Tags to Markup 8
File
src/ConvertMediaTagsToMarkup/Entity.php
View source
<?php
namespace Drupal\convert_media_tags_to_markup\ConvertMediaTagsToMarkup;
use Drupal\convert_media_tags_to_markup\traits\CommonUtilities;
use Drupal\Core\Entity\EntityInterface as DrupalEntity;
class Entity {
use CommonUtilities;
public function __construct(DrupalEntity $entity) {
$this->entity = $entity;
}
public function process(bool $simulate = TRUE, string $log = 'print_r') {
foreach ($this->entity
->getFields() as $fieldname => $field) {
$this
->processField($fieldname, $field, $simulate, $log);
}
if ($simulate) {
$log('Not actually saving entity ' . $this->entity
->id() . ' because we are in simulation mode.' . PHP_EOL);
}
else {
$log('Saving entity ' . $this->entity
->id() . ' because we are not in simulation mode.' . PHP_EOL);
$this->entity
->save();
}
}
public function processField(string $fieldname, $field, bool $simulate = TRUE, $log = 'print_r') {
$log('Processing field ' . $fieldname . ' of class ' . get_class($field) . ' for entity ' . $this->entity
->id() . PHP_EOL);
$value = $field
->getValue();
foreach ($value as $delta => $row) {
if (!empty($row['value']) && !empty($row['format'])) {
$log(' => Item at position ' . $delta . ' is a candidate for processing' . PHP_EOL);
$value[$delta]['value'] = App::instance()
->filterText($row['value']);
if ($simulate) {
$log('Simulating changing the content to: ' . PHP_EOL);
$log($value[$delta]['value']);
$log(PHP_EOL);
}
else {
$this->entity->{$fieldname} = $value;
$log('Changed its content.' . PHP_EOL);
}
}
else {
$log(' => Item at position ' . $delta . ' is not a candidate for processing' . PHP_EOL);
}
}
}
}
Classes
Name |
Description |
Entity |
Represents a Drupal entity for our purposes. |