Skip to main content
Drupal code

Main navigation

  • Home
  • Snippets
User account menu
  • Log in

Breadcrumb

  1. Home

Add new text fields to content type programmatically from CSV file

By admin , 15 November, 2024

Tags

  • field
  • node
  • csv
/**
* Add new fields.
*/
function MYMODULE_update_10000() {
 $content_type = 'article';
 $csv_file_path = DRUPAL_ROOT . '/_profile_fields.csv';
 
 $node_type = NodeType::load($content_type);
 if (!$node_type) {
   throw new \Exception("COntent type '$content_type' not found.");
 }
 
 if (!file_exists($csv_file_path)) {
   throw new \Exception("CSV file not found $csv_file_path.");
 }
 
 $fields = [];
 if (($handle = fopen($csv_file_path, 'rb')) !== FALSE) {
   $header = fgetcsv($handle, NULL, '|');
   while (($data = fgetcsv($handle, NULL, '|')) !== FALSE) {
     $fields[] = array_combine($header, $data);
   }
   fclose($handle);
 }
 
 foreach ($fields as $field) {
   $field_name = $field['machine_name'];
   $label = $field['label'];
   $description = $field['description'];
   $existing_field = FieldStorageConfig::loadByName('node', $field_name);
   if (!$existing_field) {
     FieldStorageConfig::create([
       'field_name' => strtolower($field_name),
       'entity_type' => 'node',
       'type' => 'string',
       'settings' => [
         'max_length' => 255,
       ],
     ])->save();
   }
   
   $field_instance = FieldConfig::loadByName('node', $content_type, $field_name);
   if (!$field_instance) {
     FieldConfig::create([
       'field_name' => strtolower($field_name),
       'entity_type' => 'node',
       'bundle' => $content_type,
       'label' => $label,
       'description' => $description,
       'required' => FALSE,
     ])->save();
   }
   
   \Drupal::service('entity_display.repository')->getFormDisplay('node', $content_type)
     ->setComponent($field_name, [
       'type' => 'string_textfield',
       'weight' => 0,
     ])
     ->save();
     
   \Drupal::service('entity_display.repository')->getViewDisplay('node', $content_type)
     ->setComponent($field_name, [
       'label' => 'above',
       'type' => 'string',
       'weight' => 0,
     ])
     ->save();
 }
}

Tags Cloud

dependency injection(21) form(18) drush(14) views(11) twig(11) preprocess(9) field(8) node(7) translate(7) render(6) tabs(6) controller(5) config(5) phpstorm(5) file(5) entityTypeManager(5) routes(5) comment(4) database(4) jquery(4) service(4) roles(4) phpunit(3) php(3) cache(3) entity(3) slick(3) block(3) custom entity(3) javascript(3) user(3) taxonomy(3) guzzle(3) url(3) phpstan(2) event(2) commerce(2) template(2) schema(2) migration(2) libraries(2) date(2) access(2) ajax(2) photoswipe(2) permissions(2) logger(2) vscode(2) suggestions(2) states(2) title(2) fieldset(2) redirect(2) mysql(2) git(1) request(1) attribute(1) service provider(1) csv(1) PHPDoc(1) table(1) eslint(1) deprecation(1) theme(1) errors(1) wsod(1) settings.php(1) watchdog(1) pager(1) widget(1) module(1) file.usage(1) queue(1) exif(1) imagemagick(1) image(1) media(1) uuid(1) drupal.org(1) tests(1) renderer(1) array(1) exposed form(1) page manager(1) transliteration(1) random(1) entityQuery(1) development(1) mail(1) webform(1) register(1) textarea(1) label(1) input(1) batch(1) import(1) messenger(1)
RSS feed
Powered by Drupal