I am trying to add I18n on a collection in one of my models. When I initialize the server/console and I called the collection, the translation is missing, but after reloading the app or the console, the translations appear.
Here is the example:
[1] pry(main)> Listing::CONDITIONS
=> [["translation missing: en.activerecord.attributes.listing.conditions.new",
"new"], ["translation missing: en.activerecord.attributes.listing.conditions.used-new",
"used-new"], ["translation missing: en.activerecord.attributes.listing.conditions.used-vgood",
"used-vgood"], ["translation missing: en.activerecord.attributes.listing.conditions.used-good",
"used-good"], ["translation missing: en.activerecord.attributes.listing.conditions.used-acceptable",
"used-acceptable"]]
[2] pry(main)> reload!
Reloading...
=> true
[3] pry(main)> Listing::CONDITIONS
=> [["New", "new"],
["Used - Like New", "used-new"],
["Used - Very Good", "used-vgood"],
["Used - Good", "used-good"],
["Used - Acceptable", "used-acceptable"]]
Any ideas?
Related
I have created a controller to validate and save a form.
I don't know how the individual form fields are named because this is assigned by the system in the form designer.
For this reason I first make a query ($valArrays) to find out which fields are required and then find out what the array key is.
The field order_title is always required.
$valArrays = FormDetail::where('form_details.form_id', $request->form_id)
->where('required', 'yes')
->get();
$array = array();
$array['order_title'] = 'required';
foreach ($valArrays as $valArray) :
$array[$valArray->detail_id] = 'required';
endforeach;
$request->validate($array);
when I query via dd($array) I get the following evaluation:
^ array:6 [▼
"order_title" => "required"
"QS0brKe5LP" => "required"
"TjFusWAKLh" => "required"
"uNqAPbNwkg" => "required"
"OvclFtOlvF" => "required"
"52e515mfRf" => "required"
]
so everything is ok! but the programme jumps back to the form without saving.
If I take out the code part everything works but without validation :0(
Do you have any idea why this could be?
I am working on a migrate script in D8 that pulls from a MySQL DB and will populate a table in D8 created by this module...
https://github.com/bjaxelsen/field_ipaddress
This module is a D8 port of this one...
https://www.drupal.org/project/field_ipaddress
anyways I got the module installed and added the field to the accounts with the unlimited number of values.
A general user migrate was already made and works fine. It will migrate the username, email, and status into Drupal and all is good here.
The second migration is to move a list of user IP addresses into D8. My dataset will look like this...
user_id slstatus status_id ipFrom ipTo modifyDateUnix
50374 1 0 1.1.1.1 1.1.1.1 1505415351
25108 0 1 4.4.4.0 4.4.4.255 1479243329
The real code runs the INET_ATON function in mysql to return integers, but the above to show the intent.
In the prepare row function I also take the IP Addresses and convert them into hex values, which is needed for the db table storage.
slstatus relates to the user's active status and status_id relates to the ip record's status. These are used to determine a new variable called deleted. I also make an variable called idx which is a...well an index. All of these new fields are added back to the row.
My YAML looks like this and is where my headache begins...
id: UserIpsMigrate
migration_group: 'UMG'
label: 'User IP Migration'
source:
plugin: UserIpsMigrate
process:
'field_ip_address/bundle': 'user'
'field_ip_address/deleted': deleted
'field_ip_address/entity_id':
plugin: migration_lookup
migration: UserMigrate
source: user_id
'field_ip_address/revision_id':
plugin: migration_lookup
migration: UserMigrate
source: user_id
'field_ip_address/langcode': 'en'
'field_ip_address/delta': idx
'field_ip_address/field_ip_address_ip_ipv6': 0
'field_ip_address/field_ip_address_ip_from': ipFrom
'field_ip_address/field_ip_address_ip_to': ipTo
destination:
plugin: entity:user
default_bundle: migration
migration_dependencies:
required:
- user_migrate
The errors I am getting show that this migration is trying to create a user...which will fail with this data and so I am certain I need to use a different destination plugin, but I have no idea...
1) which destination plugin that I should be using
2) if I went "off the rails" with my yaml
I found a solution.
First I changed my yaml file to look like this.
id: UserIpsMigrate
migration_group: HEUMG
label: 'User IP Migration'
source:
plugin: tUserIpsMigrate
process:
bundle:
plugin: default_value
default_value: user
deleted: deleted
entity_id:
plugin: migration_lookup
migration: UserMigrate
source: user_id
revision_id:
plugin: migration_lookup
migration: UserMigrate
source: user_id
langcode:
plugin: default_value
default_value: en
delta: delta
field_ip_address_ipv6:
plugin: default_value
default_value: 0
field_ip_address_ip_from: ipFrom
field_ip_address_ip_to: ipTo
destination:
plugin: field_ip_address
migration_dependencies:
required:
- user_migrate
I also found that the functions GetIds() are very important in determining the mapping from source to destination targets and is used by the migration system when making the migration mapping table.
Extending the sqlBase class, I had a query to pull the data need for the migration and it would use 2 keys from different tables users and ipaddresses. So my source getIds() function looked like this...
/**
* {#inheritdoc}
*/
public function getIds() {
return [
'user_id' => [
'type' => 'integer',
'alias' => 'slc',
],
'siteLicense_id' => [
'type' => 'integer',
'unsigned' => FALSE,
],
];
}
...and my destination getIds ended up as this...
/**
* {#inheritdoc}
*/
public function getIds() {
return [
'entity_id' => [
'type' => 'integer',
'unsigned' => FALSE,
],
'deleted' => [
'type' => 'integer',
'size' => 'tiny',
],
'delta' => [
'type' => 'integer',
'unsigned' => FALSE,
],
'langcode' => [
'type' => 'string',
'max_length' => 32,
'is_ascii' => TRUE,
],
];
}
With the above getIds() defined you will have a new table create once you run a drush command, like "drush migrate:status".
The table "migrate_map_" + your migration id or in my case migrate_map_useripsmigrate is created with the fields of source_id_hash, sourceid1, sourceid2, destid1, destid2, destid3, destid4, source_row_status, rollback_status, last_imported and hash. The source and destination fields are mapped to the getIds defined above and this is how the migration system keeps track of all migrations ran, from this specific migration. Other migrations will have their own tables.
As for the destination plugin itself, I had to make my own destination plugin based on extending the DestinationBase class.
Extending DestinationBase requires you to define the internals of 3 functions, import, getIds and, fields. GetIds I covered above and fields are just like defining fields in an SQL source plugin. Import is how your destination plugin will save the source data to the destination. In my case, I used an insert query like this...
/**
* {#inheritdoc}
*/
public function import(Row $row, array $old_destination_id_values = []) {
$bundle = $row->getDestinationProperty('bundle');
$deleted = $row->getDestinationProperty('deleted');
$entity_id = $row->getDestinationProperty('entity_id');
$revision_id = $row->getDestinationProperty('revision_id');
$langcode = $row->getDestinationProperty('langcode');
$delta = $row->getDestinationProperty('delta');
$field_ip_address_ipv6 = $row->getDestinationProperty('field_ip_address_ipv6');
$field_ip_address_ip_from = $row->getDestinationProperty('field_ip_address_ip_from');
$field_ip_address_ip_to = $row->getDestinationProperty('field_ip_address_ip_to');
$result = $this->con->insert('user__field_ip_address')
->fields([
'bundle' => $bundle,
'deleted' => $deleted,
'entity_id' => $entity_id,
'revision_id' => $revision_id,
'langcode' => $langcode,
'delta' => $delta,
'field_ip_address_ipv6' => $field_ip_address_ipv6,
'field_ip_address_ip_from' => $field_ip_address_ip_from,
'field_ip_address_ip_to' => $field_ip_address_ip_to,
])
->execute();
return [
'entity_id' => $entity_id,
'deleted' => $deleted,
'delta' => $delta,
'langcode' => $langcode,
];
}
The return should be the same fields as defined in your destination getIds() function and in the same order.
This may not be the most ideal method of writing a destination plugin, but it does work.
This has been driving me nuts for 2 days and I can't find an answer anywhere on Google so would really appreciate a little help..
I have a custom registration form on my website, which sends the data to a fairly standard PHPBB3 user_add process as follows:
$user_row = array(
'username' => request_var('createUsername',''),
'user_password' => phpbb_hash(request_var('createPassword','')),
'user_email' => request_var('createEmail',''),
'group_id' => '2',
'user_timezone' => '1.00',
// 'user_dst' => '0',
'user_lang' => 'en',
'user_type' => $user_type,
'user_actkey' => $user_actkey,
'user_ip' => $user->ip,
'user_regdate' => time(),
'user_inactive_reason' => $user_inactive_reason,
'user_inactive_time' => $user_inactive_time,
);
// Register user...
$user_id = user_add($user_row, $cp_data);
// If creating the user failed, display an error
if ($user_id === false)
{
trigger_error('NO_USER', E_USER_ERROR);
}
That works fine and I'm happy with it, however, I have created a custom profile field in the Admin Control Panel called 'ea_real_name' which I want to hold the user's real name. This corresponds to a field on the registration form called 'createRealName' (sent through as $_POST['createRealName'])
I know that user_add takes an optional field called 'cp_data', but I can't for the life of me work out how to format this data... Should it be an array (something like 'ea_real_name' => request_var('createRealName','') or something else?
PHPBB's wiki for the field is empty (https://wiki.phpbb.com/Custom_profile::submit_cp_field) so not much help...
Thanks! :-)
I was right in my assumption! It's an array with the field name prefixed by pf_.
Finally found an answer here: https://www.phpbb.com/community/viewtopic.php?f=71&t=1638905
$cp_data = array(
'pf_ea_real_name' => request_var('createRealName','')
);
Is the correct way to do it...
I am trying to create a bundled product programmatically, and setting the options via this:
$new_options[$count] = array(
'required' => 0,
'position' => 1,
'parent_id' => $parentProduct->getId(),
'type' => 'select',
'title' => $product->getName(),
'default_title' => $product->getName()
);
$new_selections[$count] = array(array(
'product_id' => $product->getEntityId(),
'selection_qty' => $child['qty'],
'selection_can_change_qty' => 0,
'position' => 0,
'is_default' => 1,
'selection_price_type' => 0,
'selection_price_value' => 0.0
));
...
$parentProduct->setBundleOptionsData($new_options);
$parentProduct->setBundleSelectionsData($new_selections);
Which looks correct (as described in https://stackoverflow.com/a/4415800/494643). However, it is not working - I get an SQL exception complaining that Column 'selection_id' cannot be null'. How do I get around this? The selection id is an auto_increment column, so I can't get it until it is created, but it looks like it cannot be created?
The solution lies in the _beforeSave function of the Mage_Bundle_Model_Selection model.
This causes it to attempt to write the selection_id to the catalog_product_bundle_selection_price table, before it has saved the selection object - and therefore before it has generated a selection_id. The answer therefore is to force it to generate an id before it saves, so looking again at the _beforeSave function, we can see that it only saves the price if the store is not 0.
Specifically, this part:
$storeId = Mage::registry('product')->getStoreId();
if (!Mage::helper('catalog')->isPriceGlobal() && $storeId) {
which means that if we set the store id of the registry product to 0 first, we can cause it to save the selection, without saving the price. We would then need to reset the store to the previous value, and save again, to record the price - although only after having reloaded the object to get the newly generated selection id.
I am trying to create a json file from a rake task using rabl. Below I have the simplified version to test with.
When I view 'articles.json' or 'articles/2.json' via the url, I get the expected json response.
But when I try to run it via the rake task, it always has a null value for #articles in the jsonFile created. It will render the index.json.rabl view the same number of times as #articles.count, but the values are always null.
So how do I pass in the #articles object created in my rake task to Rabl.render?
index.json.rabl
#feedName ||= 'default'
node(:rss) { partial('articles/rss), :object => #feedName }
node(:headlines) { partial('articles/show'), :object => #articles }
show.json.rabl
object #article
attributes :id,:body
....
exports.rake
task :breakingnews => :config do
filename = 'breakingnews.json'
jsonFile = File.new(filename)
#articles = Article.limit(10)
n = Rabl::renderer.json(#articles,'articles/index',view_paths => 'app/views')
jsonFile.puts b
jsonFile.close
I ran into a similar problem. You basically have 2 options:
Pass a single object explicitly as a parameter
Pass multiple objects implicitly by scope
By Parameter
In your task
#your_object = ...
Rabl.render(#your_object, 'your_template', view_paths => 'relative/path/from/project/root', :format => :json)
In your Rabl Template
object #any_name_you_like
attributes :id,:body
....
This will render your template as a json with the object specified as its instance object (you can name it anything you want)
By Scope
This is a bit more tricky. The only option I found is by setting the desired objects as instance variables in the calling scope and set this scope for the rendering of the template (see scope).
In your task
#one_object = ...
#another_object = ...
Rabl.render(nil, 'your_template', view_paths => 'relative/path/from/project/root', :format => :json, :scope => self)
In your Rabl Template
object #one_object
attributes :id,:body
node(:my_custom_node) { |m| #another_object.important_stuff }