class Entity in Convert Media Tags to Markup 8
Same name and namespace in other branches
- 2.x src/ConvertMediaTagsToMarkup/Entity.php \Drupal\convert_media_tags_to_markup\ConvertMediaTagsToMarkup\Entity
Represents a Drupal entity for our purposes.
Hierarchy
- class \Drupal\convert_media_tags_to_markup\ConvertMediaTagsToMarkup\Entity uses CommonUtilities
Expanded class hierarchy of Entity
1 file declares its use of Entity
- CommonUtilities.php in src/
traits/ CommonUtilities.php
File
- src/
ConvertMediaTagsToMarkup/ Entity.php, line 11
Namespace
Drupal\convert_media_tags_to_markup\ConvertMediaTagsToMarkupView source
class Entity {
use CommonUtilities;
/**
* Constructor.
*
* @param Drupal\Core\Entity\EntityInterface $entity
* The Drupal entity for which this object is a wrapper.
*/
public function __construct(DrupalEntity $entity) {
$this->entity = $entity;
}
/**
* Process this entity; change the media tags code to an image tag.
*
* @param bool $simulate
* Whether or not to Simulate the results.
* @param string $log
* For example "print_r" or "dpm".
*
* @throws Exception
*/
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();
}
}
/**
* Process a field.
*
* @param string $fieldname
* A field name.
* @param object $field
* A field list object.
* @param bool $simulate
* Whether or not to Simulate the results.
* @param string $log
* For example "print_r" or "dpm".
*
* @throws Exception
*/
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);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CommonUtilities:: |
public | function | Mockable wrapper around decodeEntities(). | |
CommonUtilities:: |
public | function | Mockable wrapper around Json::decode(). | |
CommonUtilities:: |
public | function | Mockable wrapper around File::load(). Also throws an exception. | |
CommonUtilities:: |
protected | function | Get all entities of a specific type and bundle. | |
CommonUtilities:: |
public | function | Log a string to the watchdog. | |
CommonUtilities:: |
public | function | Log an error to the watchdog. | |
CommonUtilities:: |
public | function | Log a \Throwable to the watchdog. | |
Entity:: |
public | function | Process this entity; change the media tags code to an image tag. | |
Entity:: |
public | function | Process a field. | |
Entity:: |
public | function | Constructor. |