pdf_to_imagefield-1.0.x-dev/pdf_to_imagefield.module

pdf_to_imagefield.module
<?php

/**
 * @file
 * Provides init steps for the PDF to Image conversion.
 */

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_module_implements_alter().
 */
function pdf_to_imagefield_module_implements_alter(&$implementations, $hook) {
  if (in_array($hook, ['entity_insert', 'entity_update'])) {
    // Move our hook implementations to the end of the list to be sure that
    // others have already done all actions around the entity.
    $group = $implementations['pdf_to_imagefield'];
    unset($implementations['pdf_to_imagefield']);
    $implementations['pdf_to_imagefield'] = $group;
  }
}

/**
 * Implements hook_form_alter().
 *
 * Hides the target image field from editors, if you don't want them to provide
 * their own snapshot.
 *
 * Should be able to run on any fielded entity type edit form, not just nodes.
 */
function pdf_to_imagefield_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (!$form_state->getFormObject() instanceof ContentEntityForm) {
    return;
  }

  $entity = $form_state->getFormObject()->getEntity();
  $entity_type = $entity->getEntityTypeId();
  $bundle = $entity->bundle();

  // Find if this entity has any fields that use the pdf_to_imagefield widget.
  if ($fields_pdf = \Drupal::service('pdf_to_imagefield.manager')->getPdfToImageSourceFields($entity_type, $bundle)) {
    foreach ($fields_pdf as $field) {
      $target_field = $field['settings']['target_field'] ?? NULL;
      if (!empty($field['settings']['hide_target_field']) && isset($form[$target_field])) {
        $form[$target_field]['#access'] = FALSE;
      }
    }
  }
}

/**
 * Implements hook_entity_insert().
 */
function pdf_to_imagefield_entity_insert(EntityInterface $entity) {
  pdf_to_imagefield_convert_pdf($entity);
}

/**
 * Implements hook_entity_update().
 */
function pdf_to_imagefield_entity_update(EntityInterface $entity) {
  pdf_to_imagefield_convert_pdf($entity);
}

/**
 * Converts PDF file to image in the entity field.
 */
function pdf_to_imagefield_convert_pdf(EntityInterface $entity): void {
  if (!$entity instanceof ContentEntityBase) {
    return;
  }
  $entity_type = $entity->getEntityTypeId();
  $bundle = $entity->bundle();

  $pdf_fields = \Drupal::service('pdf_to_imagefield.manager')->getPdfToImageSourceFields($entity_type, $bundle);
  foreach ($pdf_fields as $field_id => $field) {
    $original = [];
    $current = [];
    foreach ($entity->get($field_id)->getValue() as $file) {
      $current[] = $file['target_id'];
    }
    if (isset($entity->original) && !$entity->original->get($field_id)->isEmpty()) {
      foreach ($entity->original->get($field_id)->getValue() as $file) {
        $original[] = $file['target_id'];
      }
    }
    if (array_diff($current, $original) || array_diff($original, $current)) {
      \Drupal::service('pdf_to_imagefield.manager')->initConversion($entity, $field_id, $field);
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc