You are here

class Entity in Convert Media Tags to Markup 8

Same name and namespace in other branches
  1. 2.x src/ConvertMediaTagsToMarkup/Entity.php \Drupal\convert_media_tags_to_markup\ConvertMediaTagsToMarkup\Entity

Represents a Drupal entity for our purposes.

Hierarchy

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\ConvertMediaTagsToMarkup
View 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

Namesort descending Modifiers Type Description Overrides
CommonUtilities::decodeEntities public function Mockable wrapper around decodeEntities().
CommonUtilities::drupalJsonDecode public function Mockable wrapper around Json::decode().
CommonUtilities::fileLoad public function Mockable wrapper around File::load(). Also throws an exception.
CommonUtilities::getAllEntities protected function Get all entities of a specific type and bundle.
CommonUtilities::watchdog public function Log a string to the watchdog.
CommonUtilities::watchdogError public function Log an error to the watchdog.
CommonUtilities::watchdogThrowable public function Log a \Throwable to the watchdog.
Entity::process public function Process this entity; change the media tags code to an image tag.
Entity::processField public function Process a field.
Entity::__construct public function Constructor.