packthub_ebook_integration-1.0.0/src/Plugin/EbookDefinition.php
src/Plugin/EbookDefinition.php
<?php
namespace Drupal\packt\Plugin;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\NodeType;
class EbookDefinition {
public function __construct() {}
public function contentTypeDefinition(): array {
$entityTypeManager = \Drupal::service('entity_type.manager');
$types = [];
$contentTypes = $entityTypeManager->getStorage('node_type')->loadMultiple();
foreach ($contentTypes as $contentType) {
if($contentType instanceof NodeType) {
$types[] = [
'id'=>$contentType->id(),
'label' =>$contentType->label(),
'fields' => $this->contentFieldDefinitions($contentType->id()),
];
}
}
return $types;
}
public function contentFieldDefinitions(string $content_type):array {
$field_storage_configs = \Drupal::service('entity_field.manager')
->getFieldDefinitions('node',$content_type);
$fields = [];
foreach ($field_storage_configs as $config) {
if($config instanceof BaseFieldDefinition) {
$fields[] = [
'label' => $config->getLabel(),
'id' => $config->getName(),
'type' => $config->getType(),
];
}
if($config instanceof FieldConfig ) {
$fields[] = [
'label' => $config->getLabel(),
'id' => $config->getName(),
'type' => $config->getType(),
];
}
}
return array_filter($fields, function($item){
if($item['id'] !== 'uuid' && $item['id'] !== 'nid' && $item['id'] !== 'type'
&& $item['id'] !== 'revision_timestamp' && $item['id'] !== 'revision_uid' &&
$item['id'] !== 'revision_log' && $item['id'] !== 'status' && $item['id'] !== 'uid' &&
$item['id'] !== 'created' && $item['id'] !== 'changed' && $item['id'] !== 'sticky'
&& $item['id'] !== 'default_langcode' && $item['id'] !== 'revision_default'
&& $item['id'] !== 'revision_translation_affected' && $item['id'] !== 'path' &&
$item['id'] !== 'vid') {
return $item;
}
});
}
public function packthubFields():array {
$cacheFactory = \Drupal::cache();
$cacheId = 'packt_first_product';
$dd = $cacheFactory->get($cacheId)->data ?? NULL;
if(empty($dd)) {
$pro = new Products();
$pro->setLimit(1);
$pro->setFilter('product_type','=', 'book');
$prod = $pro->getProducts(all: FALSE);
$pro->setProductID($prod[0]['product_id']);
$dd = $pro->getProducts();
// Store the array in the cache
$cacheFactory->set($cacheId, $dd, CacheBackendInterface::CACHE_PERMANENT);
}
if(!empty($dd)) {
$fields = [
'product_id' => 'Product ID',
'product_type' => 'Product Type',
'distribution_status' => 'Distribution status',
'early_access' => 'Early access',
'sample_chapter' => 'Sample chapter',
'isbn' => 'ISBN',
];
foreach ($dd as $key=>$item) {
if($key === 'metadata') {
$fields['metadata.category.name'] = 'Product category';
$fields['metadata.publication_date'] = "Publication date";
$fields['metadata.concept.name'] = 'Product concept';
$fields['metadata.edition.edition'] = 'Product edition';
}
if($key === 'product_information') {
$fields['product_information.title'] = 'Product title';
$fields['product_information.subtitle'] = "Product subtitle";
$fields['product_information.short_description'] = 'Product short description';
$fields['product_information.description'] = 'Product description';
$fields['product_information.meta_description'] = 'Product meta description';
$fields['product_information.publisher'] = "Product publisher";
$fields['product_information.publishing_dept'] = 'Product publishing dept';
$fields['product_information.what_you_will_learn'] = 'Product what you will learn';
$fields['product_information.audience'] = 'Product audience';
$fields['product_information.approach'] = "Product approach";
$fields['product_information.key_features'] = 'Product key features';
$fields['product_information.github_repo'] = 'Product github repo';
$fields['product_information.imprint'] = 'Product imprint';
$fields['product_information.keywords'] = 'Product keywords';
}
if($key === 'contributors') {
$fields['contributors.0.firstname'] = 'Author First name';
$fields['contributors.0.lastname'] = 'Author Last name';
$fields['contributors.0.description'] = 'Author description';
$fields['contributors.0.linkedin_url'] = 'Author linkedin url';
$fields['contributors.0.profile_url'] = 'Author profile url';
}
if($key === 'chapters') {
$fields['chapters.chapter_name'] = "Chapters names (| seperated)";
$fields['chapters.chapter_number'] = "Chapters numbers (| separated)";
}
if($key === 'files') {
$fields['files.distribution_type.cover_image'] = "Ebook cover image";
$fields['files.distribution_type.epub'] = "Ebook epub file";
$fields['files.distribution_type.ebook'] = "Ebook pdf file";
}
if($key === "formats") {
$fields['formats.0.ebook.price'] = "Ebook prices (| separated)";
}
}
return $fields;
}
return [];
}
}
