You are here

public function ExifContent::entity_insert_update in Exif 8.2

Same name and namespace in other branches
  1. 8 src/ExifContent.php \Drupal\exif\ExifContent::entity_insert_update()

Main entry of the module.

Parameters

string $entityType: The entity type name to be modified.

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity to look for metadata fields.

bool $update: Indicate an Update (against an Insert).

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

File

src/ExifContent.php, line 181

Class

ExifContent
Class ExifContent make link between drupal content and file content.

Namespace

Drupal\exif

Code

public function entity_insert_update($entityType, FieldableEntityInterface $entity, $update = TRUE) {
  $bundles_to_check = $this
    ->getBundleForExifData();
  if (in_array($entity
    ->bundle(), $bundles_to_check)) {
    $exif = ExifFactory::getExifInterface();
    $ar_exif_fields = $this
      ->filterFieldsOnSettings($entityType, $entity);
    $ar_exif_fields = $exif
      ->getMetadataFields($ar_exif_fields);
    if (!$update && isset($entity->original)) {
      $original = $entity->original;
      foreach ($ar_exif_fields as $drupal_field => $metadata_field_descriptor) {
        $field_name = $drupal_field;
        $field = $entity
          ->get($field_name);
        $field
          ->offsetSet(0, $original
          ->get($field_name));
      }
    }
    else {
      $image_fields = $this
        ->getImageFields($entity);
      $metadata_images_fields = $this
        ->getImageFieldsMetadata($entity, $ar_exif_fields, $image_fields);
      foreach ($ar_exif_fields as $drupal_field => $metadata_field_descriptor) {
        $field_name = $drupal_field;
        $field = $entity
          ->get($field_name);
        $key = $metadata_field_descriptor['metadata_field']['tag'];
        $section = $metadata_field_descriptor['metadata_field']['section'];
        if (array_key_exists($metadata_field_descriptor['image_field'], $metadata_images_fields)) {
          if ($key == "all") {
            $j = 0;
            foreach ($metadata_images_fields[$metadata_field_descriptor['image_field']] as $metadata_image_fields) {
              $html = '<table class="metadata-table"><tbody>';
              foreach ($metadata_image_fields as $currentSection => $currentValues) {
                $html .= '<tr class="metadata-section"><td colspan=2>' . $currentSection . '</td></tr>';
                foreach ($currentValues as $currentKey => $currentValue) {
                  $exif_value = $this
                    ->sanitizeValue($currentValue);
                  $html .= '<tr class="metadata-value"><td>' . $currentKey . '</td><td>' . $exif_value . '</td></tr>';
                }
              }
              $html .= '</tbody><tfoot></tfoot></table>';
              $this
                ->handleTextField($j, $field, $section, $key, [
                "value" => $html,
                'format' => 'full_html',
              ]);
              $j++;
            }
          }
          else {
            $values = [];
            foreach ($metadata_images_fields[$metadata_field_descriptor['image_field']] as $metadata_image_fields) {
              if (array_key_exists($section, $metadata_image_fields) && array_key_exists($key, $metadata_image_fields[$section])) {
                $value = $metadata_image_fields[$section][$key];
                if (is_string($value) && isset($metadata_field_descriptor['metadata_field_separator'])) {
                  if (is_string($metadata_field_descriptor['metadata_field_separator'])) {
                    $subValues = explode($metadata_field_descriptor['metadata_field_separator'], $value);
                    foreach ($subValues as $index => $subValue) {
                      $values[] = $subValue;
                    }
                  }
                  else {
                    $values[] = $value;
                  }
                }
                else {
                  if (is_array($value)) {
                    $values = array_merge($values, $value);
                  }
                  else {
                    $values[] = $value;
                  }
                }
              }
            }
            $j = 0;
            foreach ($values as $innerkey => $value) {
              $this
                ->handleField($j, $field, $section, $key, $value);
              $j++;
            }
          }
        }
      }
    }
  }
}