View source
<?php
namespace Drupal\webform_content_creator;
use Drupal\node\Entity\NodeType;
use Drupal\webform\WebformSubmissionInterface;
class WebformContentCreatorUtilities {
const WEBFORM = 'webform';
const WEBFORM_SUBMISSION = 'webform_submission';
const ENTITY_TYPE_MANAGER = 'entity_type.manager';
const ENTITY_MANAGER = 'entity_field.manager';
const CONTENT_BASIC_FIELDS = [
'body',
'status',
'uid',
];
public static function existsWebformContentCreatorEntity($id) {
$entity = \Drupal::entityQuery('webform_content_creator')
->condition('id', $id)
->execute();
return (bool) $entity;
}
private static function buildTree(array $elements) {
$definitions = \Drupal::service('plugin.manager.webform.element')
->getDefinitions();
$layout_elements = [
'webform_wizard_page',
'container',
'details',
'fieldset',
'webform_flexbox',
];
$result = [];
$webform_field_ids = array_keys($elements);
$wizardPage = t('Webform elements');
$flag = 0;
$aux = [];
foreach ($webform_field_ids as $v) {
if ($v === 'actions') {
continue;
}
$title = 'Section';
if (isset($elements[$v]['#title'])) {
$title = $elements[$v]['#title'];
}
else {
if (isset($elements[$v]['#markup'])) {
$title = $elements[$v]['#markup'];
}
}
if (in_array($elements[$v]["#type"], $layout_elements, TRUE)) {
if ($elements[$v]["#webform_parent_key"] !== '') {
continue;
}
if ($flag === 0) {
$wizard_page = html_entity_decode($title);
unset($aux);
$flag++;
continue;
}
if (!empty($aux)) {
foreach ($aux as $k2 => $v2) {
$result[$wizard_page][$k2] = $v2;
}
}
$wizard_page = html_entity_decode($title);
unset($aux);
}
elseif ($elements[$v]["#webform_parent_key"] === '') {
$result['0,' . $v] = html_entity_decode($title) . ' (' . $v . ') - ' . $definitions[$elements[$v]["#type"]]['label'];
}
elseif ($elements[$v]["#type"] !== "webform_section") {
$aux['0,' . $v] = html_entity_decode($title) . ' (' . $v . ') - ' . $definitions[$elements[$v]["#type"]]['label'];
}
}
foreach ($aux as $k2 => $v2) {
$result[$wizard_page][$k2] = $v2;
}
return $result;
}
public static function getWebformElements($webform_id) {
$webform = \Drupal::entityTypeManager()
->getStorage(self::WEBFORM)
->load($webform_id);
$options = [];
$submission_storage = \Drupal::entityTypeManager()
->getStorage(self::WEBFORM_SUBMISSION);
$field_definitions = $submission_storage
->checkFieldDefinitionAccess($webform, $submission_storage
->getFieldDefinitions());
foreach ($field_definitions as $key => $field_definition) {
if (isset($field_definition['type']) && !empty($field_definition['type'])) {
$options['1,' . $key] = $field_definition['title'] . ' (' . $key . ') - ' . $field_definition['type'];
}
}
$elements = $webform
->getElementsInitializedAndFlattened();
$result = self::buildTree($elements);
$result[t('Webform properties')
->render()] = $options;
return $result;
}
public static function getWebformElementsTypes($webform_id) {
if (!isset($webform_id) || empty($webform_id)) {
return NULL;
}
$webform = \Drupal::entityTypeManager()
->getStorage(self::WEBFORM)
->load($webform_id);
if (empty($webform)) {
return NULL;
}
$submission_storage = \Drupal::entityTypeManager()
->getStorage(self::WEBFORM_SUBMISSION);
$submission_storage_definitions = $submission_storage
->getFieldDefinitions();
if (empty($submission_storage_definitions)) {
return NULL;
}
$result = $submission_storage
->checkFieldDefinitionAccess($webform, $submission_storage_definitions);
if (empty($result)) {
return NULL;
}
$elements = $webform
->getElementsInitializedAndFlattened();
if (is_array($elements)) {
$webform_field_ids = array_keys($elements);
foreach ($webform_field_ids as $v) {
if (!isset($elements[$v]) || empty($elements[$v])) {
continue;
}
$result[$v] = $elements[$v]['#type'];
}
}
return $result;
}
public static function contentTypeFields(NodeType $content_type) {
$entity_manager = \Drupal::service(self::ENTITY_MANAGER);
$fields = [];
if (!empty($content_type)) {
$fields = $entity_manager
->getFieldDefinitions('node', $content_type
->getOriginalId());
}
return $fields;
}
public static function getContentFieldsIds(NodeType $content_type) {
$node_fields = self::contentTypeFields($content_type);
$ids = array_keys($node_fields);
return array_filter($ids, function ($fid) {
return strpos($fid, 'field_') === 0 || in_array($fid, self::CONTENT_BASIC_FIELDS);
});
}
public static function getAllContentTypeIds() {
return \Drupal::service(self::ENTITY_TYPE_MANAGER)
->getStorage('node_type')
->getQuery()
->execute();
}
public static function getAllContentTypes() {
$ids = self::getAllContentTypeIds();
return \Drupal::entityTypeManager()
->getStorage('node_type')
->loadMultiple(array_keys($ids));
}
public static function getFormattedContentTypes() {
$content_types = self::getAllContentTypes();
$result = [];
foreach ($content_types as $k => $v) {
$result[$k] = $v
->label();
}
return $result;
}
public static function getAllWebformIds() {
$ids = \Drupal::entityTypeManager()
->getStorage(self::WEBFORM)
->getQuery()
->condition('template', FALSE)
->execute();
return $ids;
}
public static function getAllWebforms() {
$ids = self::getAllWebformIds();
$webforms = \Drupal::entityTypeManager()
->getStorage(self::WEBFORM)
->loadMultiple(array_keys($ids));
return $webforms;
}
public static function getFormattedWebforms() {
$webforms = self::getAllWebforms();
$result = [];
foreach ($webforms as $k => $v) {
$category = $v
->get('category');
if (empty($category)) {
$result[$k] = $v
->label();
}
else {
$result[$category][$k] = $v
->label();
}
}
return $result;
}
public static function getFormattedEncryptionProfiles() {
$profiles = [];
$module_handler = \Drupal::service('module_handler');
if ($module_handler
->moduleExists('encrypt')) {
$profiles = \Drupal::service(self::ENTITY_TYPE_MANAGER)
->getStorage('encryption_profile')
->loadMultiple();
}
$result = [];
foreach ($profiles as $k => $v) {
$result[$k] = $v
->label();
}
return $result;
}
public static function getDecryptedValue($value, $encryption_profile) {
$result = FALSE;
if (empty($value) || empty($encryption_profile)) {
return '';
}
if (\Drupal::service('module_handler')
->moduleExists('encrypt')) {
$result = \Drupal::service('encryption')
->decrypt($value, $encryption_profile);
}
if ($result === FALSE) {
$result = $value;
}
return $result;
}
public static function getTokenValue($value, $encryption_profile, WebformSubmissionInterface $webform_submission, $type = self::WEBFORM_SUBMISSION) {
if (empty($value) || empty($webform_submission)) {
return '';
}
$tokens = \Drupal::token()
->scan($value);
$token_keys = [];
$token_values = [];
if (empty($tokens)) {
return $value;
}
foreach ($tokens[$type] as $val) {
$token_value = \Drupal::token()
->replace($val, [
self::WEBFORM_SUBMISSION => $webform_submission,
]);
if (!empty($encryption_profile)) {
$token_value = self::getDecryptedValue($token_value, $encryption_profile);
}
$token_keys[] = $val;
$token_values[] = $token_value;
}
if (empty($token_values)) {
return $value;
}
return str_replace($token_keys, $token_values, $value);
}
}