Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmcintosh/3015ede37221835e4ba1612079a736fb to your computer and use it in GitHub Desktop.
Save cmcintosh/3015ede37221835e4ba1612079a736fb to your computer and use it in GitHub Desktop.
Programmatically create a field in Drupal 8.
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;

$bundles = ['user'];

$fields['user_picture'] = [
  'type' => 'image',
  'entity_type' => 'user',
  'bundle' => 'user',
  'label' => 'User picture',
  'description' => 'Your virtual face or picture.',
  'required' => FALSE,
  'widget' => [
    'type' => 'image_image',
    'settings' => [
      'progress_indicator' => 'throbber',
      'preview_image_style' => 'thumbnail',
    ],
  ],
  'formatter' => [
    'default' => [
      'type' => 'image',
      'label' => 'hidden',
      'settings' => [
        'image_style' => 'thumbnail',
        'image_link' => 'content',
      ],
    ],
  ],
  'settings' => [
    'file_extensions' => 'png gif jpg jpeg',
    'file_directory' => 'pictures/[date:custom:Y]-[date:custom:m]',
    'max_filesize' => '30 KB',
    'max_resolution' => '85x85',
    'alt_field' => FALSE,
    'title_field' => FALSE,
    'alt_field_required' => FALSE,
    'title_field_required' => FALSE,
  ],
];

foreach ($fields as $field_name => $config) {
  $field_storage = FieldStorageConfig::loadByName($config['entity_type'], $field_name);
  if (empty($field_storage)) {
    FieldStorageConfig::create(array(
      'field_name' => $field_name,
      'entity_type' => $config['entity_type'],
      'type' => $config['type'],
    ))->save();
  }
}

foreach ($bundles as $bundle) {
  foreach ($fields as $field_name => $config) {
    $config_array = array(
      'field_name' => $field_name,
      'entity_type' => $config['entity_type'],
      'bundle' => $bundle,
      'label' => $config['label'],
      'required' => $config['required'],
    );
  }

  if (isset($config['settings'])) {
    $config_array['settings'] = $config['settings'];
  }

  $field = FieldConfig::loadByName($config['entity_type'], $bundle, $field_name);
  if (empty($field) && $bundle !== "" && !empty($bundle)) {
    FieldConfig::create($config_array)->save();
  }

  if ($bundle !== "" && !empty($bundle)) {
    if (!empty($field)) {
      $field->setLabel($config['label'])->save();
      $field->setRequired($config['required'])->save();
    }
    if ($config['widget']) {
      entity_get_form_display($config['entity_type'], $bundle, 'default')
        ->setComponent($field_name, $config['widget'])
        ->save();
    }
    if ($config['formatter']) {
      foreach ($config['formatter'] as $view => $formatter) {
        $view_modes = \Drupal::entityManager()->getViewModes($config['entity_type']);
        if (isset($view_modes[$view]) || $view == 'default') {
          entity_get_display($config['entity_type'], $bundle, $view)
            ->setComponent($field_name, !is_array($formatter) ? $config['formatter']['default'] : $formatter)
            ->save();
        }
      }
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment