Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenlammerts/de0471dc210749f4afe8d2e69bab46d8 to your computer and use it in GitHub Desktop.
Save jeroenlammerts/de0471dc210749f4afe8d2e69bab46d8 to your computer and use it in GitHub Desktop.
Craft CMS 3 content migrations - create field
<?php
namespace craft\contentmigrations;
use Craft;
use craft\db\Migration;
/**
* m170831_195046_second_migration migration.
*/
class m170831_195046_field_migration extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
$fieldsService = Craft::$app->getFields();
$field = $fieldsService->createField([
'type' => 'craft\fields\PlainText',
'groupId' => 1,
'name' => 'Test field',
'handle' => 'testField',
'instructions' => '',
'translationMethod' => 'none',
'translationKeyFormat' => NULL,
'settings' => [
'placeholder' => 'Type here',
'charLimit' => '',
'multiline' => '',
'initialRows' => '4',
'columnType' => 'text',
],
]);
$success = $fieldsService->saveField($field);
return $success;
}
/**
* @inheritdoc
*/
public function safeDown()
{
$field = Craft::$app->getFields()->getFieldByHandle('testField');
$success = Craft::$app->getFields()->deleteFieldById($field->id);
return $success;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment