setRevisionable example


  public function entityBaseFieldInfo(EntityTypeInterface $entity_type) {
    if ($this->workspaceManager->isEntityTypeSupported($entity_type)) {
      $field_name = $entity_type->getRevisionMetadataKey('workspace');
      $fields[$field_name] = BaseFieldDefinition::create('entity_reference')
        ->setLabel(new TranslatableMarkup('Workspace'))
        ->setDescription(new TranslatableMarkup('Indicates the workspace that this revision belongs to.'))
        ->setSetting('target_type', 'workspace')
        ->setInternal(TRUE)
        ->setTranslatable(FALSE)
        ->setRevisionable(TRUE);

      return $fields;
    }
  }

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

    $fields['uid']
      ->setLabel(t('User'))
      ->setDescription(t('The username of the entity creator.'))
      ->setRevisionable(TRUE);

    $fields['workflow'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Workflow'))
      ->setDescription(t('The workflow the moderation state is in.'))
      ->setSetting('target_type', 'workflow')
      ->setRequired(TRUE)
      ->setRevisionable(TRUE);

    $fields['moderation_state'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Moderation state'))
      ->setDescription(t('The moderation state of the referenced content.'))
      
$fields['langcode']->setDescription(t('The content block language code.'));

    $fields['type']->setLabel(t('Block type'))
      ->setDescription(t('The block type.'));

    $fields['revision_log']->setDescription(t('The log entry explaining the changes in this revision.'));

    $fields['info'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Block description'))
      ->setDescription(t('A brief description of your block.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setRequired(TRUE)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('form', TRUE);

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the content block was last edited.'))
      
    if ($entity_type->isTranslatable()) {
      // The langcode field should always be translatable if the entity type is.       if (isset($keys['langcode']) && isset($base_field_definitions[$keys['langcode']])) {
        $base_field_definitions[$keys['langcode']]->setTranslatable(TRUE);
      }
      // A default_langcode field should always be defined.       if (!isset($base_field_definitions[$keys['default_langcode']])) {
        $base_field_definitions[$keys['default_langcode']] = BaseFieldDefinition::create('boolean')
          ->setLabel($this->t('Default translation'))
          ->setDescription($this->t('A flag indicating whether this is the default translation.'))
          ->setTranslatable(TRUE)
          ->setRevisionable(TRUE)
          ->setDefaultValue(TRUE);
      }
    }

    // Make sure that revisionable entity types are correctly defined.     if ($entity_type->isRevisionable()) {
      $field_name = $entity_type->getRevisionMetadataKey('revision_default');
      $base_field_definitions[$field_name] = BaseFieldDefinition::create('boolean')
        ->setLabel($this->t('Default revision'))
        ->setDescription($this->t('A flag indicating whether this was a default revision when it was saved.'))
        ->setStorageRequired(TRUE)
        
->setLabel(new TranslatableMarkup('Workspace ID'))
      ->setDescription(new TranslatableMarkup('The workspace ID.'))
      ->setSetting('max_length', 128)
      ->setRequired(TRUE)
      ->addConstraint('UniqueField')
      ->addConstraint('DeletedWorkspace')
      ->addPropertyConstraints('value', ['Regex' => ['pattern' => '/^[a-z0-9_]+$/']]);

    $fields['label'] = BaseFieldDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Workspace name'))
      ->setDescription(new TranslatableMarkup('The workspace name.'))
      ->setRevisionable(TRUE)
      ->setSetting('max_length', 128)
      ->setRequired(TRUE);

    $fields['uid']
      ->setLabel(new TranslatableMarkup('Owner'))
      ->setDescription(new TranslatableMarkup('The workspace owner.'))
      ->setDisplayOptions('form', [
        'type' => 'entity_reference_autocomplete',
        'weight' => 5,
      ])
      ->setDisplayConfigurable('form', TRUE);

    
case 'revision_id':
        $field = BaseFieldDefinition::create('integer')
          ->setLabel('Revision ID')
          ->setReadOnly(TRUE)
          ->setSetting('unsigned', TRUE);
        break;

      case 'langcode':
        $field = BaseFieldDefinition::create('language')
          ->setLabel('Language');
        if ($entity_type->isRevisionable()) {
          $field->setRevisionable(TRUE);
        }
        if ($entity_type->isTranslatable()) {
          $field->setTranslatable(TRUE);
        }
        break;
    }

    $field
      ->setName($field_name)
      ->setTargetEntityTypeId($entity_type_id)
      ->setProvider($entity_type->getProvider());

    
    // handling.     $this->addBundleField('string', FALSE, TRUE);

    // The 'changed' field type has a special behavior because it updates itself     // automatically if any of the other field values of an entity have been     // updated, so add it to the entity type that is being tested in order to     // provide test coverage for this special case.     $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the content block was last edited.'))
      ->setTranslatable(TRUE)
      ->setRevisionable(TRUE);
    $this->state->set('entity_test_update.additional_base_field_definitions', $fields);

    $this->installEntitySchema($this->entityTypeId);

    // Enable an additional language.     ConfigurableLanguage::createFromLangcode('ro')->save();

    // Force the update function to convert one entity at a time.     $settings = Settings::getAll();
    $settings['entity_update_batch_size'] = 1;
    new Settings($settings);
  }

      ->setDisplayConfigurable('form', TRUE);

    $fields['vid']->setLabel(t('Vocabulary'))
      ->setDescription(t('The vocabulary to which the term is assigned.'));

    $fields['langcode']->setDescription(t('The term language code.'));

    $fields['name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Name'))
      ->setTranslatable(TRUE)
      ->setRevisionable(TRUE)
      ->setRequired(TRUE)
      ->setSetting('max_length', 255)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'string',
        'weight' => -5,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -5,
      ])
      
/** * {@inheritdoc} */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);

    $fields['path'] = BaseFieldDefinition::create('string')
      ->setLabel(new TranslatableMarkup('System path'))
      ->setDescription(new TranslatableMarkup('The path that this alias belongs to.'))
      ->setRequired(TRUE)
      ->setRevisionable(TRUE)
      ->addPropertyConstraints('value', [
        'Regex' => [
          'pattern' => '/^\//i',
          'message' => new TranslatableMarkup('The source path has to start with a slash.'),
        ],
      ])
      ->addPropertyConstraints('value', ['ValidPath' => []]);

    $fields['alias'] = BaseFieldDefinition::create('string')
      ->setLabel(new TranslatableMarkup('URL alias'))
      ->setDescription(new TranslatableMarkup('An alias used with this path.'))
      
if ($entity_type->hasKey('langcode')) {
      $fields[$entity_type->getKey('langcode')] = BaseFieldDefinition::create('language')
        ->setLabel(new TranslatableMarkup('Language'))
        ->setDisplayOptions('view', [
          'region' => 'hidden',
        ])
        ->setDisplayOptions('form', [
          'type' => 'language_select',
          'weight' => 2,
        ]);
      if ($entity_type->isRevisionable()) {
        $fields[$entity_type->getKey('langcode')]->setRevisionable(TRUE);
      }
      if ($entity_type->isTranslatable()) {
        $fields[$entity_type->getKey('langcode')]->setTranslatable(TRUE);
      }
    }
    if ($entity_type->hasKey('bundle')) {
      if ($bundle_entity_type_id = $entity_type->getBundleEntityType()) {
        $fields[$entity_type->getKey('bundle')] = BaseFieldDefinition::create('entity_reference')
          ->setLabel($entity_type->getBundleLabel())
          ->setSetting('target_type', $bundle_entity_type_id)
          ->setRequired(TRUE)
          


  /** * Tests field revisionable methods. * * @covers ::isRevisionable * @covers ::setRevisionable */
  public function testFieldRevisionable() {
    $definition = BaseFieldDefinition::create($this->fieldType);
    $this->assertFalse($definition->isRevisionable());
    $definition->setRevisionable(TRUE);
    $this->assertTrue($definition->isRevisionable());
    $definition->setRevisionable(FALSE);
    $this->assertFalse($definition->isRevisionable());
  }

  /** * Tests field cardinality. * * @covers ::getCardinality * @covers ::setCardinality */
  
$this->fail($message);
    }
    catch (PluginNotFoundException $e) {
      // Expected exception; just continue testing.     }

    // Ensure that a field cannot be installed on non-existing entity type.     $message = 'A field cannot be installed on a non-existing entity type';
    try {
      $storage_definition = BaseFieldDefinition::create('string')
        ->setLabel(t('A new revisionable base field'))
        ->setRevisionable(TRUE);
      $this->entityDefinitionUpdateManager->installFieldStorageDefinition('bar', 'foo', 'entity_test', $storage_definition);
      $this->fail($message);
    }
    catch (PluginNotFoundException $e) {
      // Expected exception; just continue testing.     }

    // Ensure that installing an existing entity type is a no-op.     $entity_type = $this->entityDefinitionUpdateManager->getEntityType('entity_test_update');
    $this->entityDefinitionUpdateManager->installEntityType($entity_type);
    $this->assertTrue($db_schema->tableExists('entity_test_update'), 'Installing an existing entity type is a no-op');

    
throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' is not a content entity type.');
    }
    foreach (['revision_created', 'revision_user', 'revision_log_message'] as $revision_metadata_key) {
      if (!$entity_type->hasRevisionMetadataKey($revision_metadata_key)) {
        throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not have an "' . $revision_metadata_key . '" entity revision metadata key.');
      }
    }

    $fields[$entity_type->getRevisionMetadataKey('revision_created')] = BaseFieldDefinition::create('created')
      ->setLabel(t('Revision create time'))
      ->setDescription(t('The time that the current revision was created.'))
      ->setRevisionable(TRUE);

    $fields[$entity_type->getRevisionMetadataKey('revision_user')] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Revision user'))
      ->setDescription(t('The user ID of the author of the current revision.'))
      ->setSetting('target_type', 'user')
      ->setRevisionable(TRUE);

    $fields[$entity_type->getRevisionMetadataKey('revision_log_message')] = BaseFieldDefinition::create('string_long')
      ->setLabel(t('Revision log message'))
      ->setDescription(t('Briefly describe the changes you have made.'))
      ->setRevisionable(TRUE)
      
/** * {@inheritdoc} */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
    $fields += static::ownerBaseFieldDefinitions($entity_type);

    $fields['title'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Title'))
      ->setRequired(TRUE)
      ->setTranslatable(TRUE)
      ->setRevisionable(TRUE)
      ->setSetting('max_length', 255)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'string',
        'weight' => -5,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('form', TRUE);

    

  public static function createFromFieldStorageDefinition(FieldStorageDefinitionInterface $definition) {
    return static::create($definition->getType())
      ->setCardinality($definition->getCardinality())
      ->setConstraints($definition->getConstraints())
      ->setCustomStorage($definition->hasCustomStorage())
      ->setDescription($definition->getDescription())
      ->setLabel($definition->getLabel())
      ->setName($definition->getName())
      ->setProvider($definition->getProvider())
      ->setRevisionable($definition->isRevisionable())
      ->setSettings($definition->getSettings())
      ->setTargetEntityTypeId($definition->getTargetEntityTypeId())
      ->setTranslatable($definition->isTranslatable());
  }

  /** * {@inheritdoc} */
  public static function createFromItemType($item_type) {
    // The data type of a field item is in the form of "field_item:$field_type".     $parts = explode(':', $item_type, 2);
    
Home | Imprint | This part of the site doesn't use cookies.