Set default value for attribute in Setup Script in Magento 2
I have created install script to add an attribute to category.
Default value has to be set, thus 'default' => '1', is used.
InstallData.php file:
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
Category::ENTITY,
'my_attr',
[
'type' => 'int',
'label' => 'My',
'input' => 'select',
'default' => '1',
'source' => 'MagentoEavModelEntityAttributeSourceBoolean',
'required' => false,
'sort_order' => 100,
'group' => 'General',
]
);
}
The problem is that later when I use $cat->getMyAttr() I can get this only when I have set it to true or false in admin panel and saved settings. Otherwise I get just null.
In other words default value is not being set during setup of the module.
How to set default value that will be set to all the categories with setup script?
magento2 category attributes eav category-attribute
add a comment |
I have created install script to add an attribute to category.
Default value has to be set, thus 'default' => '1', is used.
InstallData.php file:
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
Category::ENTITY,
'my_attr',
[
'type' => 'int',
'label' => 'My',
'input' => 'select',
'default' => '1',
'source' => 'MagentoEavModelEntityAttributeSourceBoolean',
'required' => false,
'sort_order' => 100,
'group' => 'General',
]
);
}
The problem is that later when I use $cat->getMyAttr() I can get this only when I have set it to true or false in admin panel and saved settings. Otherwise I get just null.
In other words default value is not being set during setup of the module.
How to set default value that will be set to all the categories with setup script?
magento2 category attributes eav category-attribute
add a comment |
I have created install script to add an attribute to category.
Default value has to be set, thus 'default' => '1', is used.
InstallData.php file:
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
Category::ENTITY,
'my_attr',
[
'type' => 'int',
'label' => 'My',
'input' => 'select',
'default' => '1',
'source' => 'MagentoEavModelEntityAttributeSourceBoolean',
'required' => false,
'sort_order' => 100,
'group' => 'General',
]
);
}
The problem is that later when I use $cat->getMyAttr() I can get this only when I have set it to true or false in admin panel and saved settings. Otherwise I get just null.
In other words default value is not being set during setup of the module.
How to set default value that will be set to all the categories with setup script?
magento2 category attributes eav category-attribute
I have created install script to add an attribute to category.
Default value has to be set, thus 'default' => '1', is used.
InstallData.php file:
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
Category::ENTITY,
'my_attr',
[
'type' => 'int',
'label' => 'My',
'input' => 'select',
'default' => '1',
'source' => 'MagentoEavModelEntityAttributeSourceBoolean',
'required' => false,
'sort_order' => 100,
'group' => 'General',
]
);
}
The problem is that later when I use $cat->getMyAttr() I can get this only when I have set it to true or false in admin panel and saved settings. Otherwise I get just null.
In other words default value is not being set during setup of the module.
How to set default value that will be set to all the categories with setup script?
magento2 category attributes eav category-attribute
magento2 category attributes eav category-attribute
asked Oct 21 '17 at 17:50
TheKitMurkitTheKitMurkit
121112
121112
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
This is because according to Magento your module data is already installed and it doesn't update your changes in the InstallData file unless you change your module version.
Here's the solution:
Go to your module directory and look for module.xml
file in etc folder and simply change the setup version number so that the Magento knows that it has to update the data. Obviously, the setup version will be higher than current. For ex, if it's 1.0.1 then change it to 1.0.2
<module name="yourmodule_name" setup_version="1.0.1"/>
Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method
– medmek
Jan 28 at 10:40
add a comment |
Set required to true. This is because when attribute is required, it has a value all the time.
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f198080%2fset-default-value-for-attribute-in-setup-script-in-magento-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is because according to Magento your module data is already installed and it doesn't update your changes in the InstallData file unless you change your module version.
Here's the solution:
Go to your module directory and look for module.xml
file in etc folder and simply change the setup version number so that the Magento knows that it has to update the data. Obviously, the setup version will be higher than current. For ex, if it's 1.0.1 then change it to 1.0.2
<module name="yourmodule_name" setup_version="1.0.1"/>
Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method
– medmek
Jan 28 at 10:40
add a comment |
This is because according to Magento your module data is already installed and it doesn't update your changes in the InstallData file unless you change your module version.
Here's the solution:
Go to your module directory and look for module.xml
file in etc folder and simply change the setup version number so that the Magento knows that it has to update the data. Obviously, the setup version will be higher than current. For ex, if it's 1.0.1 then change it to 1.0.2
<module name="yourmodule_name" setup_version="1.0.1"/>
Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method
– medmek
Jan 28 at 10:40
add a comment |
This is because according to Magento your module data is already installed and it doesn't update your changes in the InstallData file unless you change your module version.
Here's the solution:
Go to your module directory and look for module.xml
file in etc folder and simply change the setup version number so that the Magento knows that it has to update the data. Obviously, the setup version will be higher than current. For ex, if it's 1.0.1 then change it to 1.0.2
<module name="yourmodule_name" setup_version="1.0.1"/>
This is because according to Magento your module data is already installed and it doesn't update your changes in the InstallData file unless you change your module version.
Here's the solution:
Go to your module directory and look for module.xml
file in etc folder and simply change the setup version number so that the Magento knows that it has to update the data. Obviously, the setup version will be higher than current. For ex, if it's 1.0.1 then change it to 1.0.2
<module name="yourmodule_name" setup_version="1.0.1"/>
edited Feb 8 '18 at 6:03
Piyush
4,79972053
4,79972053
answered Feb 8 '18 at 5:44
BunnyBunny
1
1
Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method
– medmek
Jan 28 at 10:40
add a comment |
Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method
– medmek
Jan 28 at 10:40
Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method
– medmek
Jan 28 at 10:40
Thank's but I had to disable/enable the module before getting my code run, but the UpgradeData runs with your method
– medmek
Jan 28 at 10:40
add a comment |
Set required to true. This is because when attribute is required, it has a value all the time.
New contributor
add a comment |
Set required to true. This is because when attribute is required, it has a value all the time.
New contributor
add a comment |
Set required to true. This is because when attribute is required, it has a value all the time.
New contributor
Set required to true. This is because when attribute is required, it has a value all the time.
New contributor
New contributor
answered 14 mins ago
Raibel Botta RigoresRaibel Botta Rigores
1
1
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f198080%2fset-default-value-for-attribute-in-setup-script-in-magento-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown