Skip to content
Snippets Groups Projects

Create config entities through Entity API

Merged Roderik Muit requested to merge issue/custom_elements-3500903:3500903-phpunit-failure into 3.x
1 file
+ 25
20
Compare changes
  • Side-by-side
  • Inline
@@ -2,6 +2,7 @@
namespace Drupal\Tests\custom_elements\Functional;
use Drupal\Core\Config\FileStorage;
use Drupal\Tests\BrowserTestBase;
use Drupal\custom_elements\CustomElement;
use Drupal\custom_elements\CustomElementGeneratorTrait;
@@ -9,7 +10,6 @@ use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
use Symfony\Component\Yaml\Parser;
/**
* Test rendering custom elements into markup.
@@ -126,28 +126,33 @@ class CustomElementsRenderMarkupTest extends BrowserTestBase {
private function importPartialThunderConfig(array $exclude_bundles) {
$config_dir = dirname(dirname(dirname(__DIR__)))
. '/modules/custom_elements_thunder/config/install';
// Avoid warnings when opendir does not have the permissions to open a
// directory.
if ($handle = opendir($config_dir)) {
while (FALSE !== ($filename = readdir($handle))) {
if (str_ends_with($filename, '.yml') && !array_filter(
$source = new FileStorage($config_dir);
// Get config names that do not contain any of $exclude_bundles.
$config_names = array_filter(
$source->listAll(),
function ($config_name) use ($exclude_bundles) {
// Return boolean indicating config name contains any of $exclude.
return !array_filter(
$exclude_bundles,
function ($bundle) use ($filename) {
return str_contains($filename, $bundle);
function ($bundle) use ($config_name) {
return str_contains($config_name, $bundle);
}
)) {
$config_name = substr($filename, 0, strlen($filename) - 4);
$data = file_get_contents("$config_dir/$filename");
$parser = new Parser();
$data = $parser->parse($data);
$config = \Drupal::configFactory()->getEditable($config_name);
foreach ($data as $data_key => $value) {
$config->set($data_key, $value);
}
$config->save();
}
);
}
);
foreach ($config_names as $config_name) {
// Use the entity API to create config entities.
$entity_type_id = \Drupal::service('config.manager')->getEntityTypeIdByName($config_name);
if ($entity_type_id) {
\Drupal::entityTypeManager()
->getStorage($entity_type_id)
->create($source->read($config_name))
->save();
}
else {
\Drupal::service('config.storage')->write($config_name, $source->read($config_name));
}
closedir($handle);
}
}
Loading