uuid example

public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form$form_state);

    $type = $this->entity;
    if ($this->operation == 'add') {
      $form['#title'] = $this->t('Add content type');
      $fields = $this->entityFieldManager->getBaseFieldDefinitions('node');
      // Create a node with a fake bundle using the type's UUID so that we can       // get the default values for workflow settings.       // @todo Make it possible to get default values without an entity.       // https://www.drupal.org/node/2318187       $node = $this->entityTypeManager->getStorage('node')->create(['type' => $type->uuid()]);
    }
    else {
      $form['#title'] = $this->t('Edit %label content type', ['%label' => $type->label()]);
      $fields = $this->entityFieldManager->getFieldDefinitions('node', $type->id());
      // Create a node to get the current values for workflow settings fields.       $node = $this->entityTypeManager->getStorage('node')->create(['type' => $type->id()]);
    }

    $form['name'] = [
      '#title' => $this->t('Name'),
      '#type' => 'textfield',
      


  /** * Tests default media name functionality. */
  public function testDefaultName() {
    // Make sure that the default name is set if not provided by the user.     /** @var \Drupal\media\MediaInterface $media */
    $media = Media::create(['bundle' => $this->testMediaType->id()]);
    $media_source = $media->getSource();
    $this->assertSame('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.');
    $this->assertSame('media:' . $media->bundle() . ':' . $media->uuid()$media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.');
    $this->assertSame('media:' . $media->bundle() . ':' . $media->uuid()$media->getName(), 'Default name was not used correctly by getName().');
    $this->assertSame($media->getName()$media->label(), 'Default name and label are not the same.');
    $media->save();
    $this->assertSame('media:' . $media->bundle() . ':' . $media->uuid()$media->getName(), 'Default name was not saved correctly.');
    $this->assertSame($media->getName()$media->label(), 'The label changed during save.');

    // Make sure that the user-supplied name is used.     /** @var \Drupal\media\MediaInterface $media */
    $name = 'User-supplied name';
    $media = Media::create([
      'bundle' => $this->testMediaType->id(),
      
    $storage = \Drupal::entityTypeManager()->getStorage('config_test');
    $empty = $storage->create();
    $this->assertNull($empty->label);
    $this->assertNull($empty->style);
    $this->assertSame($default_langcode$empty->language()->getId());

    // Verify ConfigEntity properties/methods on the newly created empty entity.     $this->assertTrue($empty->isNew());
    $this->assertNull($empty->getOriginalId());
    $this->assertSame('config_test', $empty->bundle());
    $this->assertNull($empty->id());
    $this->assertTrue(Uuid::isValid($empty->uuid()));
    $this->assertNull($empty->label());

    $this->assertNull($empty->get('id'));
    $this->assertTrue(Uuid::isValid($empty->get('uuid')));
    $this->assertNull($empty->get('label'));
    $this->assertNull($empty->get('style'));
    $this->assertSame($default_langcode$empty->language()->getId());

    // Verify Entity properties/methods on the newly created empty entity.     $this->assertSame('config_test', $empty->getEntityTypeId());
    // The URI can only be checked after saving.
/** * Tests string formatter output. */
  public function testUuidStringFormatter() {
    $entity = EntityTest::create([]);
    $entity->save();

    $uuid_field = $entity->get('uuid');

    // Verify default render.     $render_array = $uuid_field->view([]);
    $this->assertSame($entity->uuid()$render_array[0]['#context']['value'], 'The rendered UUID matches the entity UUID.');
    $this->assertStringContainsString($entity->uuid()$this->render($render_array), 'The rendered UUID found.');

    // Verify customized render.     $render_array = $uuid_field->view(['settings' => ['link_to_entity' => TRUE]]);
    $this->assertSame('link', $render_array[0]['#type']);
    $this->assertSame($entity->uuid()$render_array[0]['#title']['#context']['value']);
    $this->assertSame($entity->toUrl()->toString()$render_array[0]['#url']->toString());
    $rendered = $this->render($render_array);
    $this->assertStringContainsString($entity->uuid()$rendered, 'The rendered UUID found.');
    $this->assertStringContainsString($entity->toUrl()->toString()$rendered, 'The rendered entity URL found.');
  }

}
return [
      'dependencies' => [],
      'description' => 'Camelids are large, strictly herbivorous animals with slender necks and long legs.',
      'display_submitted' => TRUE,
      'help' => NULL,
      'langcode' => 'en',
      'name' => 'Camelids',
      'new_revision' => TRUE,
      'preview_mode' => 1,
      'status' => TRUE,
      'type' => 'camelids',
      'uuid' => $this->entity->uuid(),
    ];
  }

  /** * {@inheritdoc} */
  protected function getNormalizedPostEntity() {
    // @todo Update in https://www.drupal.org/node/2300677.     return [];
  }

  
->set('path', '/llama')
      ->save();

    return $node;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $author = User::load($this->entity->getOwnerId());
    $base_url = Url::fromUri('base:/jsonapi/node/camelids/' . $this->entity->uuid())->setAbsolute();
    $self_url = clone $base_url;
    $version_identifier = 'id:' . $this->entity->getRevisionId();
    $self_url = $self_url->setOption('query', ['resourceVersion' => $version_identifier]);
    $version_query_string = '?resourceVersion=' . urlencode($version_identifier);
    return [
      'jsonapi' => [
        'meta' => [
          'links' => [
            'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
          ],
        ],
        
    // needs to be purged.     $entity_test = EntityTest::create([
      'type' => 'entity_test',
    ]);
    $entity_test->set($field_name, 'test data');
    $entity_test->save();

    // Create a second bundle for the 'Entity test' entity type.     entity_test_create_bundle('test_bundle');

    // Get the uuid's for the field storages.     $field_storage_uuid = FieldStorageConfig::load($field_storage_id)->uuid();
    $field_storage_uuid_2 = FieldStorageConfig::load($field_storage_id_2)->uuid();

    $active = $this->container->get('config.storage');
    $sync = $this->container->get('config.storage.sync');
    $this->copyConfig($active$sync);
    $this->assertTrue($sync->delete($field_storage_config_name)new FormattableMarkup('Deleted field storage: @field_storage', ['@field_storage' => $field_storage_config_name]));
    $this->assertTrue($sync->delete($field_storage_config_name_2)new FormattableMarkup('Deleted field storage: @field_storage', ['@field_storage' => $field_storage_config_name_2]));
    $this->assertTrue($sync->delete($field_config_name)new FormattableMarkup('Deleted field: @field', ['@field' => $field_config_name]));
    $this->assertTrue($sync->delete($field_config_name_2a)new FormattableMarkup('Deleted field: @field', ['@field' => $field_config_name_2a]));
    $this->assertTrue($sync->delete($field_config_name_2b)new FormattableMarkup('Deleted field: @field', ['@field' => $field_config_name_2b]));

    


    $camelids->save();

    return $camelids;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $self_url = Url::fromUri('base:/jsonapi/node_type/node_type/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
    return [
      'jsonapi' => [
        'meta' => [
          'links' => [
            'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
          ],
        ],
        'version' => '1.0',
      ],
      'links' => [
        'self' => ['href' => $self_url],
      ],
/** * @covers ::id */
  public function testId() {
    $this->assertSame($this->values['id']$this->entity->id());
  }

  /** * @covers ::uuid */
  public function testUuid() {
    $this->assertSame($this->values['uuid']$this->entity->uuid());
  }

  /** * @covers ::isNew * @covers ::enforceIsNew */
  public function testIsNew() {
    // We provided an ID, so the entity is not new.     $this->assertFalse($this->entity->isNew());
    // Force it to be new.     $this->assertSame($this->entity, $this->entity->enforceIsNew());
    
'field_type' => 'boolean',
      'id' => 'node.camelids.promote',
      'label' => NULL,
      'langcode' => 'en',
      'required' => FALSE,
      'settings' => [
        'on_label' => 'On',
        'off_label' => 'Off',
      ],
      'status' => TRUE,
      'translatable' => TRUE,
      'uuid' => $this->entity->uuid(),
    ];
  }

  /** * {@inheritdoc} */
  protected function getNormalizedPostEntity() {
    // @todo Update in https://www.drupal.org/node/2300677.     return [];
  }

  
// And a block content entity.     $block_content = BlockContent::create([
      'info' => 'Spiffy prototype',
      'type' => 'spiffy',
    ]);
    $block_content->save();

    // Ensure the reusable block content is provided as a derivative block     // plugin.     /** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
    $block_manager = $this->container->get('plugin.manager.block');
    $plugin_id = 'block_content' . PluginBase::DERIVATIVE_SEPARATOR . $block_content->uuid();
    $this->assertTrue($block_manager->hasDefinition($plugin_id));

    // Set the block not to be reusable.     $block_content->setNonReusable();
    $block_content->save();

    // Ensure the non-reusable block content is not provided a derivative block     // plugin.     $this->assertFalse($block_manager->hasDefinition($plugin_id));
  }

}

    ];
    $output = Json::decode($this->drupalGet('/jsonapi/node/article', [
      'query' => ['filter' => $filter],
    ]));
    $this->assertSession()->statusCodeEquals(200);
    $output_uuids = array_map(function D$result) {
      return $result['id'];
    }$output['data']);
    $this->assertCount(1, $output_uuids);
    $this->assertSame([
      $node_3->uuid(),
    ]$output_uuids);

    // Checks whether the date is greater than or equal to the given timestamp.     $filter = [
      'filter_datetime' => [
        'condition' => [
          'path' => 'field_datetime',
          'operator' => '>=',
          'value' => date(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $timestamp_greater_than_or_equal_value),
        ],
      ],
    ];
'label' => 'Test',
      'targetEntityType' => 'user',
    ]);
    $entity_form_mode->save();
    return $entity_form_mode;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $self_url = Url::fromUri('base:/jsonapi/entity_form_mode/entity_form_mode/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
    return [
      'jsonapi' => [
        'meta' => [
          'links' => [
            'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
          ],
        ],
        'version' => '1.0',
      ],
      'links' => [
        'self' => ['href' => $self_url],
      ],
->fetchField();
      // There are two records for each entity, one for English and one for       // Romanian.       $this->assertEquals(6, $data_table_count);

      $base_table_row = $this->database->select('entity_test_update')
        ->fields('entity_test_update')
        ->condition('id', 1, '=')
        ->condition('langcode', 'en', '=')
        ->execute()
        ->fetchAllAssoc('id');
      $this->assertEquals($this->testEntities[1]->uuid()$base_table_row[1]->uuid);

      $data_table_row = $this->database->select('entity_test_update_data')
        ->fields('entity_test_update_data')
        ->condition('id', 1, '=')
        ->condition('langcode', 'en', '=')
        ->execute()
        ->fetchAllAssoc('id');
      $this->assertEquals('test entity - 1 - en', $data_table_row[1]->name);
      $this->assertEquals('shared table - 1 - value 1 - en', $data_table_row[1]->test_multiple_properties__value1);
      $this->assertEquals('shared table - 1 - value 2 - en', $data_table_row[1]->test_multiple_properties__value2);

      
// Get the response.     $resource_type = $this->container->get('jsonapi.resource_type.repository')->get('node', 'article');
    $response = $entity_resource->getCollection($resource_type$request);

    // Assertions.     $this->assertInstanceOf(CacheableResourceResponse::class$response);
    $this->assertInstanceOf(JsonApiDocumentTopLevel::class$response->getResponseData());
    $this->assertInstanceOf(Data::class$response->getResponseData()->getData());
    $data = $response->getResponseData()->getData();
    $this->assertCount(1, $data);
    $this->assertEquals($this->node2->uuid()$data->toArray()[0]->getId());
    $this->assertEqualsCanonicalizing(['node:2', 'node_list']$response->getCacheableMetadata()->getCacheTags());
  }

  /** * @covers ::getCollection */
  public function testGetEmptyCollection() {
    $request = Request::create('/jsonapi/node/article');
    $request->query = new InputBag(['filter' => ['id' => 'invalid']]);

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