/**
   * {@inheritdoc}
   */
  public function cloneEntity(EntityInterface $entity, EntityInterface $cloned_entity, $properties = []) {
    /** @var \Drupal\core\Config\Entity\ConfigEntityInterface $cloned_entity */
    $id_key = $this->entityTypeManager->getDefinition($this->entityTypeId)->getKey('id');
    $label_key = $this->entityTypeManager->getDefinition($this->entityTypeId)->getKey('label');

    // Set new entity properties.
    if (isset($properties['id'])) {
      if ($id_key) {
        $cloned_entity->set($id_key, $properties['id']);
      }
      unset($properties['id']);
    }

    if (isset($properties['label'])) {
      if ($label_key) {
        $cloned_entity->set($label_key, $properties['label']);
      }
      unset($properties['label']);
    }

    foreach ($properties as $key => $property) {
      $cloned_entity->set($key, $property);
    }

    $cloned_entity->save();
    return $cloned_entity;

  }
 /**
  * {@inheritdoc}
  */
 public function cloneEntity(EntityInterface $entity, EntityInterface $cloned_entity, $properties = []) {
   /** @var \Drupal\core\Entity\ContentEntityInterface $cloned_entity */
   if ($label_key = $this->entityTypeManager->getDefinition($this->entityTypeId)->getKey('label')) {
     $cloned_entity->set($label_key, $entity->label() . ' - Cloned');
   }
   $cloned_entity->save();
   return $cloned_entity;
 }
  /**
   * Helper function.
   *
   * Display a link to bundle's view mode page if user has permission.
   *
   * @param string $entity_type
   *   The type of the entity.
   * @param string $bundle
   *   The bundle of the entity.
   * @param string $view_mode
   *   A view mode.
   *
   * @return string
   *   A link to the view mode of the bundle if user has access.
   *   The view mode otherwise.
   */
  public function displayViewModeLink($entity_type, $bundle, $view_mode = 'default') {
    $display = $view_mode;

    // Get entity type object from entity type name.
    $entity_type_object = $this->entityTypeManager->getDefinition($entity_type);

    // Prepare URL parameters.
    $parameters = array(
      'view_mode_name' => $view_mode,
    );
    $parameters += FieldUI::getRouteBundleParameter($entity_type_object, $bundle);

    // Route.
    if ($view_mode == 'default') {
      $route = "entity.entity_view_display.{$entity_type}.default";
    }
    else {
      $route = "entity.entity_view_display.{$entity_type}.view_mode";
    }

    $url = Url::fromRoute($route, $parameters);
    if ($url->renderAccess($url->toRenderArray())) {
      $display = Link::fromTextAndUrl($view_mode, $url);
    }

    return $display;
  }
 /**
  * Get the entity definition for the entity to be updated.
  *
  * @param \Drupal\scheduled_updates\ScheduledUpdateTypeInterface $scheduledUpdateType
  *
  * @return array|\Drupal\Core\Entity\EntityTypeInterface|null
  * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
  */
 protected function getUpdateTypeDefinition(ScheduledUpdateTypeInterface $scheduledUpdateType)
 {
     if ($update_entity_type = $scheduledUpdateType->getUpdateEntityType()) {
         return $this->entityTypeManager->getDefinition($update_entity_type);
     }
     return NULL;
 }
Beispiel #5
0
  /**
   * Constructs a new Entity Clone form.
   *
   * @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match service.
   * @param \Drupal\Core\StringTranslation\TranslationManager $string_translation
   *   The string translation manager.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(EntityTypeManager $entity_type_manager, RouteMatchInterface $route_match, TranslationManager $string_translation) {
    $this->entityTypeManager = $entity_type_manager;
    $this->stringTranslationManager = $string_translation;

    $parameter_name = $route_match->getRouteObject()->getOption('_entity_clone_entity_type_id');
    $this->entity = $route_match->getParameter($parameter_name);

    $this->entityTypeDefinition = $entity_type_manager->getDefinition($this->entity->getEntityTypeId());
  }
 protected function getViewDisplays($contentType, $optional = false)
 {
     $view_display_definition = $this->entityTypeManager->getDefinition('entity_view_display');
     $view_display_storage = $this->entityTypeManager->getStorage('entity_view_display');
     foreach ($view_display_storage->loadMultiple() as $view_display) {
         $view_display_name = $view_display_definition->getConfigPrefix() . '.' . $view_display->id();
         $view_display_name_config = $this->getConfiguration($view_display_name);
         // Only select fields related with content type
         if ($view_display_name_config['bundle'] == $contentType) {
             $this->configExport[$view_display_name] = array('data' => $view_display_name_config, 'optional' => $optional);
             // Include dependencies in export files
             if ($dependencies = $this->fetchDependencies($view_display_name_config, 'config')) {
                 $this->resolveDependencies($dependencies, $optional);
             }
         }
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $io = new DrupalStyle($input, $output);
     $module = $input->getOption('module');
     $viewId = $input->getArgument('view-id');
     $optionalConfig = $input->getOption('optional-config');
     $includeModuleDependencies = $input->getOption('include-module-dependencies');
     $viewTypeDefinition = $this->entityTypeManager->getDefinition('view');
     $viewTypeName = $viewTypeDefinition->getConfigPrefix() . '.' . $viewId;
     $viewNameConfig = $this->getConfiguration($viewTypeName);
     $this->configExport[$viewTypeName] = array('data' => $viewNameConfig, 'optional' => $optionalConfig);
     // Include config dependencies in export files
     if ($dependencies = $this->fetchDependencies($viewNameConfig, 'config')) {
         $this->resolveDependencies($dependencies, $optionalConfig);
     }
     // Include module dependencies in export files if export is not optional
     if ($includeModuleDependencies) {
         if ($dependencies = $this->fetchDependencies($viewNameConfig, 'module')) {
             $this->exportModuleDependencies($io, $module, $dependencies);
         }
     }
     $this->exportConfigToModule($module, $io, $this->trans('commands.views.export.messages.view_exported'));
 }
 /**
  * {@inheritdoc}
  */
 protected function interact(InputInterface $input, OutputInterface $output)
 {
     $io = new DrupalStyle($input, $output);
     $config_types = $this->getConfigTypes();
     $config_name = $input->getArgument('config-name');
     if (!$config_name) {
         $config_type = $io->choiceNoList($this->trans('commands.config.export.single.questions.config-type'), array_keys($config_types), $this->trans('commands.config.export.single.options.simple-configuration'));
         $config_names = $this->getConfigNames($config_type);
         $config_name = $io->choiceNoList($this->trans('commands.config.export.single.questions.config-name'), array_keys($config_names));
         if ($config_type !== 'system.simple') {
             $definition = $this->entityTypeManager->getDefinition($config_type);
             $config_name = $definition->getConfigPrefix() . '.' . $config_name;
         }
         $input->setArgument('config-name', $config_name);
     }
     $module = $input->getOption('module');
     if ($module) {
         $optionalConfig = $input->getOption('optional-config');
         if (!$optionalConfig) {
             $optionalConfig = $io->confirm($this->trans('commands.config.export.single.questions.optional-config'), true);
             $input->setOption('optional-config', $optionalConfig);
         }
     }
 }
 /**
  * Tests the getDefinition() method with an invalid definition.
  *
  * @covers ::getDefinition
  *
  * @expectedException \Drupal\Component\Plugin\Exception\PluginNotFoundException
  * @expectedExceptionMessage The "pear" entity type does not exist.
  */
 public function testGetDefinitionInvalidException()
 {
     $this->setUpEntityTypeDefinitions();
     $this->entityTypeManager->getDefinition('pear', TRUE);
 }