Repository URL to install this package:
|
Version:
1.48.1 ▾
|
<?php
namespace Drupal\customeredit;
use Drupal\Component\Discovery\YamlDiscovery;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
class CustomerTextsBuilder {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
protected $themeHandler;
protected $connection;
/**
* Constructs the RouteBuilder using the passed MatcherDumperInterface.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $themeHandler
* @param \Drupal\Core\Database\Connection $connection
*/
public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $themeHandler, Connection $connection) {
$this->moduleHandler = $module_handler;
$this->themeHandler = $themeHandler;
$this->connection = $connection;
}
protected function checkIfExists($table, $id) {
if (is_array($id)) {
return ($this->connection->select($table)->fields($table, ['machine_name'])->condition('machine_name', $id[0])->condition('langcode', $id[1])->execute()->fetchField() == $id[0]);
} elseif (is_string($id)) {
return ($this->connection->select($table)->fields($table, ['machine_name'])->condition('machine_name', $id)->execute()->fetchField() == $id);
} else {
return FALSE;
}
}
public function rebuild() {
//die(var_dump($this->getTextDefinitions()));
$table = 'customeredit';
$label_table = 'customeredit_labels';
$text_table = 'customeredit_texts';
$langcode = 'en';
//$langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
$this->connection->truncate($table)->execute();
$this->connection->truncate($label_table)->execute();
$names = $this->connection->select($text_table)->fields($text_table, ['machine_name'])->execute()->fetchCol();
$new_names = [];
foreach ($this->getTextDefinitions() as $module => $texts) {
foreach ($texts as $id => $text) {
$new_names[] = $id;
$values = [
'type' => (is_string($text['type'])) ? $text['type'] : 'textfield',
'translatable' => (isset($text['translatable'])) ? intval($text['translatable']) : TRUE,
'location_url' => (isset($text['location_url'])) ? $text['location_url'] : '',
'location_selector' => (isset($text['location_selector'])) ? $text['location_selector'] : '',
'token_types' => (isset($text['token-types'])) ? implode(',', $text['token-types']) : '',
'clear_cache' => (isset($text['clear_cache'])) ? intval($text['clear_cache']) : TRUE,
];
$label_values = [
'title' => (isset($text['title'])) ? $text['title'] : $id,
'description' => (isset($text['description'])) ? $text['description'] : '',
'category' => (isset($text['category'])) ? $text['category'] : t('Miscellaneous', ['langcode' => $langcode]),
'subcategory' => (isset($text['subcategory'])) ? $text['subcategory'] : '',
];
$values['machine_name'] = $id;
$this->connection->insert($table)->fields($values)->execute();
$label_values['machine_name'] = $id;
$label_values['langcode'] = $langcode;
$this->connection->insert($label_table)->fields($label_values)->execute();
if (!$this->checkIfExists($text_table, [$id, $langcode])) {
if (is_array($text['default'])) {
$data = json_encode($text['default']);
} else {
$data = (is_string($text['default'])) ? $text['default'] : '';
}
$this->connection->insert($text_table)->fields(['machine_name' => $id, 'langcode' => $langcode, 'data' => $data])->execute();
}
}
}
$forgotten_names = array_diff($names, $new_names);
if (!empty($forgotten_names)) {
foreach ($forgotten_names as $id) {
$this->connection->delete($text_table)->condition('machine_name', $id)->execute();
}
}
}
/**
* Retrieves all defined routes from .routing.yml files.
*
* @return array
* The defined routes, keyed by provider.
*/
function getTextDefinitions() {
// Always instantiate a new YamlDiscovery object so that we always search on
// the up-to-date list of modules.
$directories = array_merge($this->moduleHandler->getModuleDirectories(), $this->themeHandler->getThemeDirectories());
$discovery = new YamlDiscovery('customertexts', $directories);
return $discovery->findAll();
}
}