Skip to content

Instantly share code, notes, and snippets.

@arjenblokzijl
Created May 18, 2016 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arjenblokzijl/256fa8de3c2d76e69fe0d8395f1b897b to your computer and use it in GitHub Desktop.
Save arjenblokzijl/256fa8de3c2d76e69fe0d8395f1b897b to your computer and use it in GitHub Desktop.
Save fields in ProcessWire from .csv file
<?php
// Bootstrap ProcessWire
include 'index.php';
// Process CSV
$rows = array_map('str_getcsv', file('fields.csv'));
$header = array_shift($rows);
$fields = array();
foreach ($rows as $row) {
$fields[] = array_combine($header, $row);
}
// Get fieldgroup
$fg = wire('templates')->get('product')->fieldgroup;
$i = 1;
foreach ($fields as $field) {
// Create field
$f = new Field();
// For example save these properties
// $f->type = wire('modules')->get($field['type']);
// $f->name = $field['name'] . '_skip_' . $i++;
// $f->label = $field['label'];
// $f->tags = $field['tags'];
// $f->useRoles = 1;
// $f->viewRoles = explode('|', $field['viewRoles']); // Explode on | to store ProcessWire arrays
// $f->editRoles = explode('|', $field['editRoles']); // Explode on | to store ProcessWire arrays
// First save field
$f->save();
// Add field to fieldgroup
$fg->add($f);
// Save fieldgroep
$fg->save();
// Output on screen
echo 'done: ' . $f->name . "<hr>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment