Commit 7a36b7df authored by Kristiaan Van den Eynde's avatar Kristiaan Van den Eynde
Browse files

Issue #3292166 by kristiaanvandeneynde: Add a way to wrap config entities

parent 8849294d
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -249,3 +249,14 @@ function group_update_9207() {
    $group_content_type->save(TRUE);
  }
}

/**
 * Introduce the group_config_wrapper entity type.
 */
function group_update_9208() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  if (!$definition_update_manager->getEntityType('group_config_wrapper')) {
    $entity_type = \Drupal::entityTypeManager()->getDefinition('group_config_wrapper');
    $definition_update_manager->installEntityType($entity_type);
  }
}
+42 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\group\Entity\Access;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Access controller for the ConfigWrapper entity.
 *
 * @see \Drupal\group\Entity\ConfigWrapper.
 */
class ConfigWrapperAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type);
  }

  /**
   * {@inheritdoc}
   */
  protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
    return AccessResult::forbidden('Config wrappers should only exist in code.')->setCacheMaxAge(Cache::PERMANENT);
  }

  /**
   * {@inheritdoc}
   */
  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
    return AccessResult::forbidden('Config wrappers should only exist in code.')->setCacheMaxAge(Cache::PERMANENT);
  }

}
+76 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\group\Entity;

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Defines the ConfigWrapper entity.
 *
 * @ingroup group
 *
 * @ContentEntityType(
 *   id = "group_config_wrapper",
 *   label = @Translation("Config wrapper"),
 *   label_singular = @Translation("config wrapper"),
 *   label_plural = @Translation("config wrappers"),
 *   label_count = @PluralTranslation(
 *     singular = "@count config wrapper",
 *     plural = "@count config wrappers"
 *   ),
 *   handlers = {
 *     "access" = "Drupal\group\Entity\Access\ConfigWrapperAccessControlHandler",
 *     "storage_schema" = "Drupal\group\Entity\Storage\ConfigWrapperStorageSchema",
 *   },
 *   base_table = "group_config_wrapper",
 *   entity_keys = {
 *     "id" = "id",
 *     "bundle" = "bundle",
 *   },
 * )
 */
class ConfigWrapper extends ContentEntityBase implements ConfigWrapperInterface {

  /**
   * {@inheritdoc}
   */
  public function getConfigEntity() {
    return $this->get('entity_id')->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function getConfigEntityId() {
    return $this->get('entity_id')->target_id;
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);

    $fields['entity_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Config'))
      ->setDescription(t('The config entity to wrap.'))
      // Required to force a string data type.
      ->setSetting('target_type', 'group_type')
      ->setRequired(TRUE)
      ->setReadOnly(TRUE);

    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
    $fields['entity_id'] = clone $base_field_definitions['entity_id'];
    $fields['entity_id']->setSetting('target_type', $bundle);
    return $fields;
  }

}
+30 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\group\Entity;

use Drupal\Core\Entity\ContentEntityInterface;

/**
 * Provides an interface defining a ConfigWrapper entity.
 *
 * @ingroup group
 */
interface ConfigWrapperInterface extends ContentEntityInterface {

  /**
   * Returns the wrapped config entity.
   *
   * @return \Drupal\Core\Config\Entity\ConfigEntityInterface
   *   The wrapped config entity.
   */
  public function getConfigEntity();

  /**
   * Returns the wrapped config entity ID.
   *
   * @return string
   *   The wrapped config entity ID.
   */
  public function getConfigEntityId();

}
+43 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\group\Entity\Storage;

use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
use Drupal\Core\Field\FieldStorageDefinitionInterface;

/**
 * Defines the config wrapper schema handler.
 */
class ConfigWrapperStorageSchema extends SqlContentEntityStorageSchema {

  /**
   * {@inheritdoc}
   */
  protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
    $schema = parent::getEntitySchema($entity_type, $reset);

    if ($base_table = $this->storage->getBaseTable()) {
      $schema[$base_table]['indexes'] += [
        $this->getEntityIndexName($entity_type, 'load_by_config') => ['bundle', 'entity_id'],
      ];
    }

    return $schema;
  }

  /**
   * {@inheritdoc}
   */
  protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) {
    $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping);

    $field_name = $storage_definition->getName();
    if ($field_name === 'entity_id') {
      $schema['fields'][$field_name]['not null'] = TRUE;
    }

    return $schema;
  }

}
Loading