|
|
|
@ -2,11 +2,16 @@
|
|
|
|
|
|
|
|
|
|
namespace Drupal\islandora_text_extraction\Plugin\Field\FieldFormatter; |
|
|
|
|
|
|
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface; |
|
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
|
|
|
use Drupal\Core\Field\FieldDefinitionInterface; |
|
|
|
|
use Drupal\Core\Field\FieldItemInterface; |
|
|
|
|
use Drupal\Core\Field\FieldItemListInterface; |
|
|
|
|
use Drupal\Core\Field\FormatterBase; |
|
|
|
|
use Drupal\Core\Form\FormStateInterface; |
|
|
|
|
use Drupal\file\Entity\File; |
|
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
|
|
|
use Drupal\Core\Render\RendererInterface; |
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Plugin implementation of the 'ocr_txt_formatter' formatter. |
|
|
|
@ -17,7 +22,62 @@ use Drupal\file\Entity\File;
|
|
|
|
|
* field_types = {"file"} |
|
|
|
|
* ) |
|
|
|
|
*/ |
|
|
|
|
class OcrTextFormatter extends FormatterBase { |
|
|
|
|
class OcrTextFormatter extends FormatterBase implements ContainerFactoryPluginInterface { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* The entity type manager. |
|
|
|
|
* |
|
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
|
|
|
|
*/ |
|
|
|
|
protected $entityTypeManager; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* OcrTextFormatter constructor. |
|
|
|
|
* |
|
|
|
|
* @param string $plugin_id |
|
|
|
|
* The plugin_id for the formatter. |
|
|
|
|
* @param mixed $plugin_definition |
|
|
|
|
* The plugin implementation definition. |
|
|
|
|
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition |
|
|
|
|
* The definition of the field to which the formatter is associated. |
|
|
|
|
* @param array $settings |
|
|
|
|
* The formatter settings. |
|
|
|
|
* @param string $label |
|
|
|
|
* The formatter label display setting. |
|
|
|
|
* @param string $view_mode |
|
|
|
|
* The view mode. |
|
|
|
|
* @param array $third_party_settings |
|
|
|
|
* Third party settings. |
|
|
|
|
* @param \Drupal\Core\Render\RendererInterface $renderer |
|
|
|
|
* The renderer. |
|
|
|
|
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
|
|
|
|
* The factory for configuration objects. |
|
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
|
|
|
|
* The entity type manager. |
|
|
|
|
*/ |
|
|
|
|
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, RendererInterface $renderer, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) { |
|
|
|
|
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $renderer, $config_factory); |
|
|
|
|
|
|
|
|
|
$this->entityTypeManager = $entity_type_manager; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* {@inheritdoc} |
|
|
|
|
*/ |
|
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
|
|
|
return new static( |
|
|
|
|
$plugin_id, |
|
|
|
|
$plugin_definition, |
|
|
|
|
$configuration['field_definition'], |
|
|
|
|
$configuration['settings'], |
|
|
|
|
$configuration['label'], |
|
|
|
|
$configuration['view_mode'], |
|
|
|
|
$configuration['third_party_settings'], |
|
|
|
|
$container->get('renderer'), |
|
|
|
|
$container->get('config.factory'), |
|
|
|
|
$container->get('entity_type.manager'), |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* {@inheritdoc} |
|
|
|
@ -70,7 +130,7 @@ class OcrTextFormatter extends FormatterBase {
|
|
|
|
|
*/ |
|
|
|
|
protected function viewValue(FieldItemInterface $item) { |
|
|
|
|
$fileItem = $item->getValue(); |
|
|
|
|
$file = File::load($fileItem['target_id']); |
|
|
|
|
$file = $this->entityTypeManager->getStorage('file')->load($fileItem['target_id']); |
|
|
|
|
$contents = file_get_contents($file->getFileUri()); |
|
|
|
|
if (mb_detect_encoding($contents) != 'UTF-8') { |
|
|
|
|
$contents = utf8_encode($contents); |
|
|
|
|