fieldUIAddNewField example

$this->assertEquals($number_of_links$number_of_links_found);
  }

  /** * Tests adding a new field. * * @todo Assert properties can be set in the form and read back in * $field_storage and $fields. */
  public function createField() {
    // Create a test field.     $this->fieldUIAddNewField('admin/structure/types/manage/' . $this->contentType, $this->fieldNameInput, $this->fieldLabel);
  }

  /** * Tests editing an existing field. */
  public function updateField() {
    $field_id = 'node.' . $this->contentType . '.' . $this->fieldName;
    // Go to the field edit page.     $this->drupalGet('admin/structure/types/manage/' . $this->contentType . '/fields/' . $field_id . '/storage');
    $this->assertSession()->assertEscaped($this->fieldLabel);

    
break;
      }
    }

    $this->assertSession()->statusCodeEquals(200);
    $this->clickLink('Create a new field');
    $this->assertSession()->statusCodeEquals(200);

    // Create a simple textfield.     $field_name = mb_strtolower($this->randomMachineName());
    $field_label = $this->randomMachineName();
    $this->fieldUIAddNewField(NULL, $field_name$field_label, 'text');
    $field_name = 'field_' . $field_name;

    // Check preview field can be ordered.     $this->drupalGet('admin/structure/contact/manage/' . $contact_form . '/form-display');
    $this->assertSession()->pageTextContains('Preview');

    // Check that the field is displayed.     $this->drupalGet('contact/' . $contact_form);
    $this->assertSession()->pageTextContains($field_label);

    // Submit the contact form and verify the content.
'weight' => mt_rand(0, 10),
    ]);
    $vocabulary->save();
    $this->vocabulary = $vocabulary->id();
  }

  /** * Tests switching view modes to use custom or 'default' settings'. */
  public function testViewModeCustom() {
    // Create a field, and a node with some data for the field.     $this->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, 'test', 'Test field');
    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
    // For this test, use a formatter setting value that is an integer unlikely     // to appear in a rendered node other than as part of the field being tested     // (for example, unlikely to be part of the "Submitted by ... on ..." line).     $value = 12345;
    $settings = [
      'type' => $this->type,
      'field_test' => [['value' => $value]],
    ];
    $node = $this->drupalCreateNode($settings);

    
// Revoke access comments permission from anon user, grant post to     // authenticated.     user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
    user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['post comments', 'skip comment approval']);

    // Create a new field.     $this->addDefaultCommentField('node', 'article');

    $name = strtolower($this->randomMachineName());
    $label = $this->randomMachineName();
    $storage_edit = ['settings[uri_scheme]' => 'private'];
    $this->fieldUIAddNewField('admin/structure/comment/manage/comment', $name$label, 'file', $storage_edit);

    // Manually clear cache on the tester side.     \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();

    // Create node.     $edit = [
      'title[0][value]' => $this->randomMachineName(),
    ];
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);

    
'description' => $description,
      'settings[link_type]' => (int) $link_type,
    ];
    if (!empty($default_uri)) {
      $field_edit['set_default_value'] = '1';
      $field_edit['default_value_input[field_' . $field_name . '][0][uri]'] = $default_uri;
      $field_edit['default_value_input[field_' . $field_name . '][0][title]'] = 'Default title';
    }
    $storage_edit = [
      'cardinality_number' => $cardinality,
    ];
    $this->fieldUIAddNewField($type_path$field_name$label, 'link', $storage_edit$field_edit);

    // Load the formatter page to check that the settings summary does not     // generate warnings.     // @todo Mess with the formatter settings a bit here.     $this->drupalGet("$type_path/display");
    $this->assertSession()->pageTextContains('Link text trimmed to 80 characters');

    // Make the fields visible in the form display.     $form_display_id = implode('.', ['node', $this->contentType->id(), 'default']);
    $form_display = EntityFormDisplay::load($form_display_id);
    $form_display->setComponent($field_name['region' => 'content']);
    
    $bundle_path = 'admin/structure/types/manage/' . $this->type;

    // Generate a random field name, must be only lowercase characters.     $field_name = strtolower($this->randomMachineName());

    $storage_edit = $field_edit = [];
    $storage_edit['settings[target_type]'] = $target_type;
    foreach ($bundles as $bundle) {
      $field_edit['settings[handler_settings][target_bundles][' . $bundle . ']'] = TRUE;
    }

    $this->fieldUIAddNewField($bundle_path$field_name, NULL, 'entity_reference', $storage_edit$field_edit);

    // Returns the generated field name.     return $field_name;
  }

  /** * Checks if a select element contains the specified options. * * @param string $name * The field name. * @param array $expected_options * An array of expected options. * * @internal */
'id' => 'foobar',
      'label' => 'Foobar',
      'description' => '',
      'target_entity_type_id' => 'entity_test',
    ]);
    $bundle->save();

    // Add a new comment field.     $storage_edit = [
      'settings[comment_type]' => 'foobar',
    ];
    $this->fieldUIAddNewField('entity_test/structure/entity_test', 'foobar', 'Foobar', 'comment', $storage_edit);

    // Add a third comment field.     $this->fieldUIAddNewField('entity_test/structure/entity_test', 'barfoo', 'BarFoo', 'comment', $storage_edit);

    // Check the field contains the correct comment type.     $field_storage = FieldStorageConfig::load('entity_test.field_barfoo');
    $this->assertInstanceOf(FieldStorageConfig::class$field_storage);
    $this->assertEquals('foobar', $field_storage->getSetting('comment_type'));
    $this->assertEquals(1, $field_storage->getCardinality());

    // Test the new entity commenting inherits default.
$field_label = $this->randomMachineName();
    $field_name_input = 'test';
    $field_name = 'field_test';

    // Create an additional node type.     $type_name1 = strtolower($this->randomMachineName(8)) . '_test';
    $type1 = $this->drupalCreateContentType(['name' => $type_name1, 'type' => $type_name1]);
    $type_name1 = $type1->id();

    // Create a new field.     $bundle_path1 = 'admin/structure/types/manage/' . $type_name1;
    $this->fieldUIAddNewField($bundle_path1$field_name_input$field_label);

    // Create an additional node type.     $type_name2 = strtolower($this->randomMachineName(8)) . '_test';
    $type2 = $this->drupalCreateContentType(['name' => $type_name2, 'type' => $type_name2]);
    $type_name2 = $type2->id();

    // Add a field to the second node type.     $bundle_path2 = 'admin/structure/types/manage/' . $type_name2;
    $this->fieldUIAddExistingField($bundle_path2$field_name$field_label);

    \Drupal::service('module_installer')->install(['views']);
    
Home | Imprint | This part of the site doesn't use cookies.