setEntity example


  protected function getFormObject(RouteMatchInterface $route_match$form_arg) {
    // If no operation is provided, use 'default'.     $form_arg .= '.default';
    [$entity_type_id$operation] = explode('.', $form_arg);

    $form_object = $this->entityTypeManager->getFormObject($entity_type_id$operation);

    // Allow the entity form to determine the entity object from a given route     // match.     $entity = $form_object->getEntityFromRouteMatch($route_match$entity_type_id);
    $form_object->setEntity($entity);

    return $form_object;
  }

}
$item->setSupportText($config['supportText']);
                $item->setHelpText($config['helpText']);
                $item->setDisplayInBackend((bool) $config['displayInBackend']);
                $item->setReadonly((bool) $config['readonly']);
                $item->setLabel($config['label']);
                $item->setPosition((int) $config['position']);
                $item->setCustom((bool) $config['custom']);
                $item->setTranslatable((bool) $config['translatable']);
                $item->setConfigured(true);
                $item->setDbalType($column->getType()->getName());
                $item->setSqlType($this->typeMapping->unifiedToSQL($item->getColumnType()));
                $item->setEntity($config['entity']);
                $item->setArrayStore($config['arrayStore']);
                $item->setElasticSearchType($this->typeMapping->unifiedToElasticSearch($config['columnType']));
                $item->setDefaultValue($config['defaultValue'] === 'NULL' ? null : $config['defaultValue']);
            }
            $items[] = $item;
        }

        usort($itemsfunction DConfigurationStruct $a, ConfigurationStruct $b) {
            if ($a->getPosition() === null && $b->getPosition() !== null) {
                return 1;
            }
            
$this->state->set('entity_test.untranslatable_fields.default_translation_affected', $default_translation_affected);
    $this->entityTypeBundleInfo->clearCachedBundles();

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->entityTypeManager->getStorage($this->entityTypeId)->create();
    if (!$default_translation) {
      $entity = $entity->addTranslation($this->translationLangcode);
    }
    $entity->save();

    $form_object = $this->entityTypeManager->getFormObject($this->entityTypeId, 'default');
    $form_object->setEntity($entity);

    $form_state = new FormState();
    $form_state
      ->addBuildInfo('callback_object', $form_object)
      ->set(['content_translation', 'translation_form']$translation_form);
    if ($is_submitted) {
      $form_state->setSubmitted();
    }
    $form_state->setRebuild($is_rebuilding);

    $handler = $this->entityTypeManager->getHandler($this->entityTypeId, 'translation');
    
/** * Tests that entity forms can be stored in the form cache. */
  public function testFormCacheForEntityForms() {
    $this->initializeWorkspacesModule();
    $this->switchToWorkspace('stage');

    $form_builder = $this->container->get('form_builder');

    $form = $this->entityTypeManager->getFormObject('entity_test_mulrevpub', 'default');
    $form->setEntity(EntityTestMulRevPub::create([]));

    $form_state = new FormState();
    $built_form = $form_builder->buildForm($form$form_state);
    $form_builder->setCache($built_form['#build_id']$built_form$form_state);
  }

  /** * Tests that non-entity forms can be stored in the form cache. */
  public function testFormCacheForRegularForms() {
    $this->initializeWorkspacesModule();
    

  public function getTypeLabel() {
    $base_entity_info = $this->entityTypeManager->getDefinition($this->pluginDefinition['base_entity_type']);
    return $this->t('@label fields', ['@label' => $base_entity_info->getLabel()]);
  }

  /** * {@inheritdoc} */
  public function setEntity(ConfigEntityInterface $entity) {
    if (parent::setEntity($entity)) {

      // Field storage config can also contain translatable values. Add the name       // of the config as well to the list of configs for this entity.       /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
      $field_storage = $this->entity->getFieldStorageDefinition();
      /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type_info */
      $entity_type_info = $this->entityTypeManager->getDefinition($field_storage->getEntityTypeId());
      $this->addConfigName($entity_type_info->getConfigPrefix() . '.' . $field_storage->id());
      return TRUE;
    }
    return FALSE;
  }

      '#weight' => -900,
    ];
  }

  /** * {@inheritdoc} */
  public function buildEntity(array $form, FormStateInterface $form_state) {
    // \Drupal\Core\Entity\EntityForm::buildEntity() clones the entity object.     // Keep it in sync with the one used by the section storage.     $this->setEntity($this->sectionStorage->getContextValue('display'));
    $entity = parent::buildEntity($form$form_state);
    $this->sectionStorage->setContextValue('display', $entity);
    return $entity;
  }

  /** * {@inheritdoc} */
  public function getEntityFromRouteMatch(RouteMatchInterface $route_match$entity_type_id) {
    $route_parameters = $route_match->getParameters()->all();

    
$container->get('entity_type.manager'),
      $container->get('language_manager'),
      $container->get('event_dispatcher')
    );
  }

  /** * {@inheritdoc} */
  public function populateFromRouteMatch(RouteMatchInterface $route_match) {
    $entity = $route_match->getParameter($this->entityType);
    $this->setEntity($entity);
    parent::populateFromRouteMatch($route_match);
  }

  /** * Gets the entity instance for this mapper. * * @return \Drupal\Core\Config\Entity\ConfigEntityInterface * The configuration entity. */
  public function getEntity() {
    return $this->entity;
  }

  public static function createFromEntity(ResourceType $resource_type, EntityInterface $entity, LinkCollection $links = NULL) {
    $resource_object = new static(
      $entity,
      $resource_type,
      $entity->uuid(),
      $resource_type->isVersionable() && $entity instanceof RevisionableInterface ? $entity->getRevisionId() : NULL,
      static::extractFieldsFromEntity($resource_type$entity),
      static::buildLinksFromEntity($resource_type$entity$links ?: new LinkCollection([]))
    );
    $resource_object->setEntity($entity);
    return $resource_object;
  }

  /** * Gets the decorated entity. * * @return \Drupal\Core\Entity\EntityInterface * The label for which to only normalize its label. */
  public function getEntity() {
    return $this->entity;
  }
use Drupal\Core\Config\Entity\ConfigEntityInterface;

/** * Provides a configuration mapper for node types. */
class NodeTypeMapper extends ConfigEntityMapper {

  /** * {@inheritdoc} */
  public function setEntity(ConfigEntityInterface $entity) {
    parent::setEntity($entity);

    // Adds the title label to the translation form.     $node_type = $entity->id();
    $config = $this->configFactory->get("core.base_field_override.node.$node_type.title");
    if (!$config->isNew()) {
      $this->addConfigName($config->getName());
    }
  }

}
$this->entityType->set('entity_keys', ['bundle' => $definition['bundle']]);

    $entity = $this->getMockForAbstractClass('Drupal\Core\Entity\EntityBase', [[]$definition['entity_type']], '', TRUE, TRUE, TRUE, ['getEntityType', 'bundle']);

    $entity->expects($this->any())
      ->method('getEntityType')
      ->willReturn($this->entityType);
    $entity->expects($this->any())
      ->method('bundle')
      ->willReturn($definition['bundle']);

    $this->entityForm->setEntity($entity);
    $this->entityForm->setOperation($definition['operation']);

    $this->assertSame($expected$this->entityForm->getFormId());
  }

  /** * Provides test data for testFormId(). */
  public function providerTestFormIds() {
    return [
      [
        
string $shopUrl,
        string $appVersion,
        string $entity,
        string $action,
        array $ids,
        ?string $appSecret,
        string $shopId,
        string $actionId
    ) {
        $this->setAction($action);
        $this->setAppVersion($appVersion);
        $this->setEntity($entity);
        $this->setIds($ids);
        $this->setShopUrl($shopUrl);
        $this->setTargetUrl($targetUrl);
        $this->setAppSecret($appSecret);
        $this->setShopId($shopId);
        $this->setActionId($actionId);
    }

    public function getTargetUrl(): string
    {
        return $this->targetUrl;
    }
->getFormDisplay($entity_type$entity_type);
    $form = [];
    $form_state = new FormState();
    $display->buildForm($entity$form$form_state);

    // Pretend the form has been built.     $form_state->setFormObject(\Drupal::entityTypeManager()->getFormObject($entity_type, 'default'));
    \Drupal::formBuilder()->prepareForm('field_test_entity_form', $form$form_state);
    \Drupal::formBuilder()->processForm('field_test_entity_form', $form$form_state);

    // Validate the field constraint.     $form_state->getFormObject()->setEntity($entity)->setFormDisplay($display$form_state);
    $entity = $form_state->getFormObject()->buildEntity($form$form_state);
    $display->validateFormValues($entity$form$form_state);

    $errors = $form_state->getErrors();
    $this->assertEquals('Widget constraint has failed.', $errors['name'], 'Constraint violation at the field items list level is generated correctly');
    $this->assertEquals('Widget constraint has failed.', $errors['test_field'], 'Constraint violation at the field items list level is generated correctly for an advanced widget');
  }

  /** * Gets the form errors for a given entity. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity * @param array $hidden_fields * (optional) A list of hidden fields. * * @return array * The form errors. */

  public function __construct(EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $form_builder) {
    $this->entityTypeManager = $entity_type_manager;
    $this->formBuilder = $form_builder;
  }

  /** * {@inheritdoc} */
  public function getForm(EntityInterface $entity$operation = 'default', array $form_state_additions = []) {
    $form_object = $this->entityTypeManager->getFormObject($entity->getEntityTypeId()$operation);
    $form_object->setEntity($entity);

    $form_state = (new FormState())->setFormState($form_state_additions);
    return $this->formBuilder->buildForm($form_object$form_state);
  }

}
      /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
      $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
      /** @var \Drupal\Core\Entity\ContentEntityInterface $new_revision */
      $new_revision = $storage->createRevision($entity, FALSE);

      // Restore the revision ID as other modules may expect to find it still       // populated. This will reset the "new revision" flag, however the entity       // object will be marked as a new revision again on submit.       // @see \Drupal\Core\Entity\ContentEntityForm::buildEntity()       $revision_key = $new_revision->getEntityType()->getKey('revision');
      $new_revision->set($revision_key$new_revision->getLoadedRevisionId());
      $form_object->setEntity($new_revision);
    }
  }

  /** * Alters bundle forms to enforce revision handling. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * @param string $form_id * The form id. * * @see hook_form_alter() */

  protected function createMediaTypeViaForm($source_plugin_id$field_name) {
    /** @var \Drupal\media\MediaTypeInterface $type */
    $type = MediaType::create(['source' => $source_plugin_id]);

    $form = $this->container->get('entity_type.manager')
      ->getFormObject('media_type', 'add')
      ->setEntity($type);

    $form_state = new FormState();
    $form_state->setValues([
      'label' => 'Test type',
      'id' => $source_plugin_id,
      'op' => 'Save',
    ]);

    /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
    $field_manager = \Drupal::service('entity_field.manager');

    
Home | Imprint | This part of the site doesn't use cookies.