View source
<?php
namespace Drupal\realistic_dummy_content_api\Framework;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
use Drupal\Component\Utility\Timer;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\Node;
if (!defined('WATCHDOG_ERROR')) {
define('WATCHDOG_ERROR', 3);
}
class Drupal8 extends Framework implements FrameworkInterface {
public function hookEntityPresave($entity) {
try {
$type = $this
->getEntityType($entity);
if (realistic_dummy_content_api_is_dummy($entity, $type)) {
$candidate = $entity;
realistic_dummy_content_api_improve_dummy_content($candidate, $type);
realistic_dummy_content_api_validate($candidate, $type);
$entity = $candidate;
}
} catch (\Throwable $e) {
\Drupal::messenger()
->addMessage($this
->t('realistic_dummy_content_api failed to modify dummy objects: message: @m', [
'@m' => $e
->getMessage(),
]), 'error', FALSE);
}
}
public function fieldInfoField($field) {
$fields = $this
->fieldInfoFields();
return $fields[$field];
}
public function develGenerate(array $info) {
if ($info['entity_type'] == 'node') {
$info['entity_type'] = 'content';
}
$info = array_merge([
'pass' => 'some-password',
'time_range' => 0,
'roles' => [],
], $info);
$plugin_manager = \Drupal::service('plugin.manager.develgenerate');
$instance = $plugin_manager
->createInstance($info['entity_type'], []);
$instance
->generate($info);
}
public function getEntityType($entity) {
return $entity
->getEntityType()
->id();
}
public function frameworkSpecificTests(&$errors, &$tests) {
$node = $this
->createDummyNode();
$result = $this
->getEntityType($node);
if ($result == 'node') {
$tests[] = 'Drupal8::getEntityType() works as expected.';
}
else {
$errors[] = 'Drupal8::getEntityType() returned ' . $result;
}
}
public function createDummyNode() {
$entity_type = 'node';
$bundle = 'article';
$entity_def = \Drupal::entityTypeManager()
->getDefinition($entity_type);
$new_node = [
'title' => 'test node',
$entity_def
->get('entity_keys')['bundle'] => $bundle,
];
$new_post = \Drupal::entityTypeManager()
->getStorage($entity_type)
->create($new_node);
$new_post
->save();
return Node::load($this
->latestId());
}
public function moduleInvokeAll($hook) {
$args = func_get_args();
$hook = array_shift($args);
return \Drupal::moduleHandler()
->invokeAll($hook, $args);
}
public function entityIsDummy($entity, $type) {
$return = isset($entity->devel_generate);
return $return;
}
public function fieldInfoFields() {
$return = [];
$field_map = \Drupal::service('entity_field.manager')
->getFieldMap();
foreach ($field_map as $entity_type => $fields) {
foreach ($fields as $field => $field_info) {
$return[$field]['entity_types'][$entity_type] = $entity_type;
$return[$field]['field_name'] = $field;
$return[$field]['type'] = $field_info['type'];
$return[$field]['bundles'][$entity_type] = $field_info['bundles'];
$this
->addFieldSettings($return, $field, $field_info);
}
}
return $return;
}
public function addFieldSettings(array &$return, $field, array $field_info) {
if ($field_info['type'] == 'entity_reference') {
if (isset($field_info['bundles']) && count($field_info['bundles'])) {
$bundle = array_pop($field_info['bundles']);
$config = FieldConfig::loadByName('node', $bundle, $field);
if ($config !== NULL) {
$settings = $config
->getSettings();
if (isset($settings['handler_settings']['target_bundles'])) {
foreach ($settings['handler_settings']['target_bundles'] as $target) {
$return[$field]['settings']['allowed_values'][] = [
'vocabulary' => $target,
];
}
}
}
}
}
}
public function moduleList() {
$full_list = \Drupal::moduleHandler()
->getModuleList();
return array_keys($full_list);
}
public function configGet($name, $default = NULL) {
$candidate = \Drupal::config('realistic_dummy_content_api')
->get($name);
return $candidate ? $candidate : $default;
}
public function alter($type, &$data, &$context1 = NULL, &$context2 = NULL, &$context3 = NULL) {
return \Drupal::moduleHandler()
->alter($type, $data, $context1, $context2);
}
public function getBundleName($entity) {
return $entity
->bundle();
}
public function stateGet($name, $default = NULL) {
return \Drupal::state()
->get($name, $default);
}
public function setEntityProperty(&$entity, $property, $value) {
if (!isset($value['set'])) {
$value = (array) $value;
$value['set'] = $value;
}
$entity
->set($property, $value['set']);
if (isset($value['options']['format'])) {
$entity->{$property}->format = $value['options']['format'];
}
unset($entity->{$property}->height);
unset($entity->{$property}->width);
}
public function formatProperty($type, $value, array $options = []) : array {
switch ($type) {
case 'file':
return [
'set' => $value
->id(),
'options' => $options,
];
case 'value':
case 'tid':
case 'text_with_summary':
return [
'set' => $value,
'options' => $options,
];
default:
throw new \Exception('Unknown property type ' . $type);
}
}
public function moduleExists($module) {
return \Drupal::moduleHandler()
->moduleExists($module);
}
public function watchdog($message, $severity = 5) {
if ($severity == WATCHDOG_ERROR) {
\Drupal::logger('realistic_dummy_content_api')
->error($message);
}
else {
\Drupal::logger('realistic_dummy_content_api')
->notice($message);
}
}
public function fieldTypeMachineName(array $info) {
$machine_name = isset($info['machine_name']) ? $info['machine_name'] : NULL;
$entity = isset($info['entity']) ? $info['entity'] : NULL;
$field_name = isset($info['field_name']) ? $info['field_name'] : NULL;
if ($machine_name == 'entity_reference' && $entity && $field_name) {
$settings = $entity
->getFieldDefinition($field_name)
->getSettings();
if (isset($settings['target_type']) && $settings['target_type'] == 'taxonomy_term') {
return 'taxonomy_term_reference';
}
}
return $info['machine_name'];
}
public function debug($message, $info = NULL) {
$this
->watchdog('<pre>' . print_r([
$info => $message,
], TRUE) . '</pre>');
}
public function createEntity(array $info = []) {
if (isset($info['entity_type']) && $info['entity_type'] != 'node') {
throw new \Exception(__FUNCTION__ . ' unknown entity type.');
}
else {
return $this
->createDummyNode();
}
}
public function entityProperties($entity) {
return [];
}
public function getEntityProperty(&$entity, $property) {
if ($property == 'title') {
return $entity
->getTitle();
}
throw new \Exception(__FUNCTION__ . ' should not be called as Drupal8 does not use properties. ' . $property);
}
public function getPath($type, $name) {
return drupal_get_path($type, $name);
}
public function frameworkRoot() {
return DRUPAL_ROOT;
}
public function getAllVocabularies() {
return Vocabulary::loadMultiple();
}
public function hookUserInsert(&$edit, $account, $category) {
}
public function hookUserPresave(&$edit, $account, $category) {
}
public function vocabularyIdentifier($vocabulary) {
return $vocabulary
->id();
}
public function newVocabularyTerm($vocabulary, $name) {
$term = Term::create([
'name' => $name,
'vid' => $vocabulary
->id(),
]);
$term
->save();
return $term;
}
public function timerStart($id) {
return Timer::start($id);
}
public function timerStop($id) {
return Timer::stop($id);
}
public function filteredHtml() {
return 'basic_html';
}
public function taxonomyLoadTree($vocabulary) {
$terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadTree($this
->vocabularyIdentifier($vocabulary));
$tids = array_map(function ($a) {
return $a->tid;
}, $terms);
return Term::loadMultiple($tids);
}
public function termId($term) {
return $term
->id();
}
public function termName($term) {
return $term
->getName();
}
public function variableDel($variable) {
}
public function vocabularyMachineName($vocabulary) {
return $this
->vocabularyIdentifier($vocabulary);
}
}