You are here

public static function PhoneNumberSettingsForm::createNewField in SMS Framework 8

Create a new field storage and field, and modify form display.

Parameters

string $entity_type_id: An entity type ID.

string $bundle: A bundle ID.

string $config_key: A config ID as found in sms.phone.*.*.fields.$config_key.

Return value

\Drupal\field\FieldConfigInterface A field config entity.

2 calls to PhoneNumberSettingsForm::createNewField()
PhoneNumberSettings::createPhoneNumberField in src/Plugin/migrate/destination/PhoneNumberSettings.php
Creates a phone number field.
PhoneNumberSettingsForm::save in src/Form/PhoneNumberSettingsForm.php
Form submission handler for the 'save' action.

File

src/Form/PhoneNumberSettingsForm.php, line 299

Class

PhoneNumberSettingsForm
Form controller for phone number settings.

Namespace

Drupal\sms\Form

Code

public static function createNewField($entity_type_id, $bundle, $config_key) {
  $entity_type_manager = \Drupal::entityTypeManager();

  // Definitions for field_storage_config.
  $definitions['field_storage']['phone_number']['type'] = 'telephone';
  $definitions['field_storage']['automated_opt_out']['type'] = 'boolean';

  // Definitions for field_config.
  $definitions['field']['phone_number']['label'] = t('Phone number');
  $definitions['field']['automated_opt_out'] = [
    'label' => t('Opt out of SMS messages.'),
    'description' => t('SMS messages requested by you are exempt.'),
    'settings' => [
      'on_label' => t('Do not receive SMS messages from this site.'),
      'off_label' => t('Receive SMS messages from this site.'),
    ],
  ];

  // Definitions for form displays.
  $definitions['form_display']['phone_number']['type'] = 'sms_telephone';
  $definitions['form_display']['automated_opt_out']['type'] = 'boolean_checkbox';

  // Generate a unique field name.
  $i = 1;
  $field_name = $config_key;
  while ($entity_type_manager
    ->getStorage('field_storage_config')
    ->load($entity_type_id . '.' . $field_name)) {
    $i++;
    $field_name = $config_key . '_' . $i;
  }

  /** @var \Drupal\field\FieldStorageConfigInterface $field_storage_config */
  $field_storage_config = $entity_type_manager
    ->getStorage('field_storage_config')
    ->create([
    'entity_type' => $entity_type_id,
    'field_name' => $field_name,
  ] + $definitions['field_storage'][$config_key]);
  $field_storage_config
    ->save();
  $field_config = $entity_type_manager
    ->getStorage('field_config')
    ->create([
    'entity_type' => $entity_type_id,
    'bundle' => $bundle,
    'field_name' => $field_name,
  ] + $definitions['field'][$config_key]);
  $field_config
    ->save();

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display */
  $entity_form_display = $field_storage_config = $entity_type_manager
    ->getStorage('entity_form_display')
    ->load($entity_type_id . '.' . $bundle . '.default');

  // Only modify display if it already exists. Don't attempt to create it.
  if ($entity_form_display) {
    $entity_form_display
      ->setComponent($field_name, $definitions['form_display'][$config_key]);
    $entity_form_display
      ->save();
  }
  return $field_config;
}