You are here

public function MobileNumberFieldTest::createField in Mobile Number 8

Same name and namespace in other branches
  1. 2.0.x src/Tests/MobileNumberFieldTest.php \Drupal\mobile_number\Tests\MobileNumberFieldTest::createField()

Create mobile number field.

3 calls to MobileNumberFieldTest::createField()
MobileNumberFieldTest::testNumberUniqueness in src/Tests/MobileNumberFieldTest.php
Test number validation.
MobileNumberFieldTest::testNumberValidity in src/Tests/MobileNumberFieldTest.php
Test number validation.
MobileNumberFieldTest::testVerification in src/Tests/MobileNumberFieldTest.php
Test number verification.

File

src/Tests/MobileNumberFieldTest.php, line 339

Class

MobileNumberFieldTest
Mobile number field functionality.'.

Namespace

Drupal\mobile_number\Tests

Code

public function createField($content_type, $field_name, $unique, $cardinality, $verify = MobileNumberUtilInterface::MOBILE_NUMBER_VERIFY_OPTIONAL, $allowed_countries = []) {
  $entity_type_manager = \Drupal::entityTypeManager();

  /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
  $field_storage = $entity_type_manager
    ->getStorage('field_storage_config')
    ->create([
    'entity_type' => 'node',
    'field_name' => $field_name,
    'type' => 'mobile_number',
  ]);
  $field_storage
    ->setSetting('unique', $unique);
  $field_storage
    ->setCardinality($cardinality)
    ->save();

  // Create the instance on the bundle.
  $instance = [
    'field_name' => $field_name,
    'entity_type' => 'node',
    'label' => 'Mobile Number',
    'bundle' => $content_type,
    'required' => TRUE,
  ];
  FieldConfig::create($instance)
    ->setSetting('verify', $verify)
    ->setSetting('countries', $allowed_countries)
    ->save();

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display */
  $entity_form_display = EntityFormDisplay::load('node.' . $content_type . '.default');
  if (!$entity_form_display) {
    $entity_form_display = EntityFormDisplay::create([
      'targetEntityType' => 'node',
      'bundle' => $content_type,
      'mode' => 'default',
      'status' => TRUE,
    ]);
  }
  $entity_form_display
    ->save();
  $entity_form_display
    ->setComponent($field_storage
    ->getName(), [
    'type' => 'mobile_number_default',
  ])
    ->save();
}