createFromNameAndData example


  public function get($name) {
    $data = $this->configStorage->read($name);
    if ($data === FALSE) {
      // For a typed config the data MUST exist.       $data = [];
      trigger_error(new FormattableMarkup('Missing required data for typed configuration: @config', [
        '@config' => $name,
      ]), E_USER_ERROR);
    }
    return $this->createFromNameAndData($name$data);
  }

  /** * {@inheritdoc} */
  public function buildDataDefinition(array $definition$value$name = NULL, $parent = NULL) {
    // Add default values for data type and replace variables.     $definition += ['type' => 'undefined'];

    $replace = [];
    $type = $definition['type'];
    
FilterFormat::create([
      'format' => 'dummy',
      'name' => 'Dummy',
    ])->save();
    $editor = Editor::create([
      'format' => 'dummy',
      'editor' => 'ckeditor5',
      'settings' => $ckeditor5_settings,
      'image_upload' => [],
    ]);

    $typed_config = $this->typedConfig->createFromNameAndData(
      $editor->getConfigDependencyName(),
      $editor->toArray(),
    );
    $violations = $typed_config->validate();

    $actual_violations = [];
    foreach ($violations as $violation) {
      $actual_violations[$violation->getPropertyPath()] = (string) $violation->getMessage();
    }
    $this->assertSame($expected_violations$actual_violations);

    

  private function validateConfiguration(array $configuration): ?string {
    if (!isset($this->schema)) {
      $configuration_name = sprintf("ckeditor5.plugin.%s", $this->id);
      // TRICKY: SchemaCheckTrait::checkConfigSchema() dynamically adds a       // 'langcode' key-value pair that is irrelevant here. Also,       // ::checkValue() may (counter to its docs) trigger an exception.       $this->configName = 'STRIP';
      $this->schema = $this->getTypedConfig()->createFromNameAndData($configuration_name$configuration);
    }

    $schema_errors = [];
    foreach ($configuration as $key => $value) {
      try {
        $schema_error = $this->checkValue($key$value);
      }
      catch (\InvalidArgumentException $e) {
        $schema_error = [$key => $e->getMessage()];
      }
      $schema_errors = array_merge($schema_errors$schema_error);
    }
    // https://www.drupal.org/project/drupal/issues/2945635.     throw new \BadMethodCallException('Method not supported');
  }

  /** * Gets typed data for config entity. * * @return \Drupal\Core\TypedData\ComplexDataInterface * The typed data. */
  protected function getConfigTypedData() {
    return $this->getTypedConfigManager()->createFromNameAndData($this->entity->getConfigDependencyName()$this->entity->toArray());
  }

}

  public function getTranslatableDefaultConfig($name) {
    if ($this->isSupported($name)) {
      // Create typed configuration wrapper based on install storage data.       $data = $this->defaultConfigStorage->read($name);
      $typed_config = $this->typedConfigManager->createFromNameAndData($name$data);
      if ($typed_config instanceof TraversableTypedDataInterface) {
        return $this->getTranslatableData($typed_config);
      }
    }
    return [];
  }

  /** * Gets translatable configuration data for a typed configuration element. * * @param \Drupal\Core\TypedData\TypedDataInterface $element * Typed configuration element. * * @return array|\Drupal\Core\StringTranslation\TranslatableMarkup * A nested array matching the exact structure under $element with only the * elements that are translatable wrapped into a TranslatableMarkup. If the * provided $element is not traversable, the return value is a single * TranslatableMarkup. */
// We'd like to verify that the top-level type is either config_base,     // config_entity, or a derivative. The only thing we can really test though     // is that the schema supports having langcode in it. So add 'langcode' to     // the data if it doesn't already exist.     if (!isset($config_data['langcode'])) {
      $config_data['langcode'] = 'en';
    }
    $this->configName = $config_name;
    if (!$typed_config->hasConfigSchema($config_name)) {
      return FALSE;
    }
    $this->schema = $typed_config->createFromNameAndData($config_name$config_data);
    $errors = [];
    foreach ($config_data as $key => $value) {
      $errors[] = $this->checkValue($key$value);
    }
    $errors = array_merge(...$errors);
    if (empty($errors)) {
      return TRUE;
    }
    return $errors;
  }

  
/** * Gets the schema wrapper for the whole configuration object. * * The schema wrapper is dependent on the configuration name and the whole * data structure, so if the name or the data changes in any way, the wrapper * should be reset. * * @return \Drupal\Core\Config\Schema\Element */
  protected function getSchemaWrapper() {
    if (!isset($this->schemaWrapper)) {
      $this->schemaWrapper = $this->typedConfigManager->createFromNameAndData($this->name, $this->data);
    }
    return $this->schemaWrapper;
  }

  /** * Validate the values are allowed data types. * * @param string $key * A string that maps to a key within the configuration data. * @param mixed $value * Value to associate with the key. * * @return null * * @throws \Drupal\Core\Config\UnsupportedDataTypeConfigException * If the value is unsupported in configuration. */

  public static function validatePair(EditorInterface $text_editor, FilterFormatInterface $text_format, bool $all_compatibility_problems = TRUE): ConstraintViolationListInterface {
    if ($text_editor->getEditor() !== 'ckeditor5') {
      throw new \InvalidArgumentException('This text editor is not configured to use CKEditor 5.');
    }

    $typed_config_manager = \Drupal::getContainer()->get('config.typed');
    $typed_config = $typed_config_manager->createFromNameAndData(
      'ckeditor5_valid_pair__format_and_editor',
      [
        // A mix of:         // - editor.editor.*.settings — note that "settings" is top-level in         // editor.editor.*, and so it is here, so all validation constraints         // will continue to work fine.         'settings' => $text_editor->toArray()['settings'],
        // - filter.format.*.filters — note that "filters" is top-level in         // filter.format.*, and so it is here, so all validation constraints         // will continue to work fine.         'filters' => $text_format->toArray()['filters'],
        
$this->assertInstanceOf(ComplexDataInterface::class$sequence);
    $this->assertInstanceOf(StringInterface::class$sequence->get('hum1'));
    $this->assertEquals('hum1', $sequence->get('hum1')->getValue());
    $this->assertEquals('hum2', $sequence->get('hum2')->getValue());
    $this->assertCount(2, $sequence->getIterator());
    // Verify the item metadata is available.     $this->assertInstanceOf(SequenceDataDefinition::class$sequence->getDataDefinition());

    // Test accessing typed config objects for simple config and config     // entities.     $typed_config_manager = \Drupal::service('config.typed');
    $typed_config = $typed_config_manager->createFromNameAndData('config_test.validation', \Drupal::configFactory()->get('config_test.validation')->get());
    $this->assertInstanceOf(TypedConfigInterface::class$typed_config);
    $this->assertEquals(['_core', 'llama', 'cat', 'giraffe', 'uuid']array_keys($typed_config->getElements()));
    $this->assertSame('config_test.validation', $typed_config->getName());
    $this->assertSame('config_test.validation', $typed_config->getPropertyPath());
    $this->assertSame('config_test.validation.llama', $typed_config->get('llama')->getPropertyPath());

    $config_test_entity = \Drupal::entityTypeManager()->getStorage('config_test')->create([
      'id' => 'asterix',
      'label' => 'Asterix',
      'weight' => 11,
      'style' => 'test_style',
    ]);
Home | Imprint | This part of the site doesn't use cookies.