Created
August 31, 2017 20:33
-
-
Save jeroenlammerts/de0471dc210749f4afe8d2e69bab46d8 to your computer and use it in GitHub Desktop.
Craft CMS 3 content migrations - create field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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