I'm attempting to get the allowed_values of a non-translatable list_string field. The allowed_values of this field are translated though and we want to get those allowed_values into a specific language & not the current UI one.
Here is my draft attempt:
// Override language before loading field configuration.
$this->languageManager->setConfigOverrideLanguage($this->languageManager->getLanguage('fr'));
// Load the field configuration in the language forced before.
$field_info = $this->fieldConfigStorage->load('profile.customer.field_title');
$label = $field_info->getLabel();
$allowed_values = $field_info->getSetting('allowed_values');
// Dump values for debugging.
dump($label);
dump($field_info);
dump($allowed_values);
With my current code, I get the proper forced label (here fr) but when I use the ::getSetting('allowed_values') I receive the allowed_values in the current UI language instead of forced one.
Does any one have any idea ?
Many thanks.
Related
I installed the computed fields modules and I'm trying to make a computed field hook for the below.
But I am not sure whether I put the code in the right place.
I just added the code below to the existing compute_field_api.php that comes with the compute field module. Is this the correct place to put this hook?
It doesn't seem to work and it doesn't display.
function computed_field_field_rating_average_compute($entity_type_manager, $entity, $fields, $delta)
{
// Get rating fields to compute
$facilities_and_services = $entity->field_facilities_and_services->value;
$fairway_rating = $entity->field_fairway_rating->value;
$recommendable_to_friends_rating = $entity->field_recommendable_to_friends->value;
$food_rating = $entity->field_food_rating->value;
$value_rating = $entity->field_value_rating->value;
$english_rating = $entity->field_english_rating->value;
$layout_rating = $entity->field_layout_rating->value;
$quality_rating = $entity->field_quality_rating->value;
$greens_rating = $entity->field_greens_rating->value;
$length_rating = $entity->field_length_rating->value;
// Set Computed field value
$value = ($facilities_and_services + $fairway_rating + $recommendable_to_friends_rating + $food_rating + $value_rating + $english_rating + $layout_rating + $quality_rating + $greens_rating + $length_rating) / 10;
return $value;
}
I tried to clear all the caches after adding this code. But it doesn't seem to work.
Had the same issue. Had to do the following:
Installed the module per the readme instructions (follow those).
Create the computed field function as you have above inside an enabled module (you may need to create a custom module for that). Note that the name of your custom module should not precede the function name (so it shouldn't be mymodule_computed_field_field_rating_average_compute, but rather as you have it above - i.e., how you've written your function is fine).
Flushed all caches.
Edit and save the node(s) where the new value should appear. That causes it to create a row in the field table.
After that, the computed value should appear in the saved content, and in views for that resaved content.
Note that it doesn't seem to be dynamic (like the old dynamic fields in D6 used to be), so it won't just magically appear for all existing content. To achieve that en-mass, I had to do a bit of back-end SQL to avoid having to manually re-save every entity (didn't want to do that for over 120 of 'em). Still figuring out if I have to do some other trickery to keep the field up-to-date despite my cache settings, as the value I'm attempting to display is dynamic, and not dependent upon the node in which the field exists...
i'm trying to add a plugin in a placeholder of a page when i save a model. But i don't know how to pass the parameter "data" to add_plugin() function of the API.
This is how i'm calling the function
page = create_page(self.title, 'page.html', 'es', parent=query_pages[0])
placeholder = page.placeholders.get(slot='News Header')
add_plugin(placeholder, 'ArticlePluginPublisher', 'es', **query_art[0].article)
And when i call add_plugin appears the next error
add_plugin() argument after ** must be a mapping, not Article
Did you check the docs? **query_art[0].article doesn't match the expected arguments as it's an instance;
placeholder (cms.models.placeholdermodel.Placeholder instance) – Placeholder to add the plugin to
plugin_type (string or cms.plugin_base.CMSPluginBase sub-class, must be a valid plugin) – What type of plugin to add
language (string) – Language code for this plugin, must be in LANGUAGES
position (string) – Position to add this plugin to the placeholder, must be a valid django-mptt position
target – Parent plugin. Must be plugin instance
data (kwargs) – Data for the plugin type instance
Data is expected to be something like a dictionary, or something that supports the ** unpacking syntax;
add_plugin(placeholder, 'ArticlePluginPublisher', 'es', **{'article': query_art[0].article})
Check out this example of data or kwargs in a function call;
https://stackoverflow.com/a/1769475/1199464
I added an additional field with a type "File" to an "article" content type. Now I need to get a fid of the uploaded file in hook_node_view(). I use for this the next code:
$my_form['fid']['#value'] = $node->field_image_svg[$node->language][0]['fid'];
The problem is that when I check "Include file in display" before saving a node, I get a fid correctly. But when I do not check, I get a warning:
Notice: Undefined offset: 0 in testmodule_node_view()
What's the problem? Is there way to get a fid of an uploaded file not depending on whether the file is displayed or not?
You are using the Enable Display field setting for the file field:
When the "Include file in display" checkbox is checked, you allow it to be rendered in the view mode of the node and so your field gets added into the language array of the field_image_svg field:
$node->field_image_svg[$node->language][0]['fid']
If unchecked it will not be allowed to and the language array will not keep the file information:
$node->field_image_svg[$node->language]
So if you want to get the fid not depending on that checkbox and still use it as a setting, you are forced to make a check on wether it exists or not to get rid of the notice and instead get the fid with:
$fid = db_query('SELECT fid FROM {file_usage} WHERE id = :id',array(':id' => $node->nid))->fetchField();
Another solution would be to remove the checbox (in field settings) and manage it to be hidden or shown through Manage display. This will keep the file's information in hook_node_view.
Hope this helps.
In my multilanguage Drupal 6 website I need to make my CCK field default values translatable, i.e.
Hello - in the English version
Bonjour - in the French one
I cannot find this text with Translation Table, neither are these values in variable table so that I can use multilingual variables.
Do You know how to have different default values in different languages?
When you define the CCK field, you can enter a PHP code snippet to override the default value of the field.
Enter the following there :
return array(0 => array('value' => t('Hello')));
Now access a node add page with this CCK field from a non-English version so that it gets added to translatable strings.
Now you're able to translate it using the "Translate interface" menu (it might take a visit to the "create" page of your cck type first though). It doesn't require any extra modules in fact, just basic D6 (and it probably works in D5 and D7 as well).
This method is a bit of a hack. Im not sure I would deploy this without really considering the consequences. For a simple usecase it MIGHT be ok.
Create a custom module, lets say def_translate. To def_translate.module, add a function
function def_translate_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if($node->type == "sometype"
&& $op == "load"
&& $node->field_some_cck_field_type[0]['value'] == "your default value")
{
$node->field_some_cck_field_type[0]['value'] =
t($node->field_some_cck_field_type[0]['value']);
}
}
Ok - so what is this doing?
When a node is loaded, hook_nodeapi gets called, with $op set to "load". This gives us an opportunity to do manipulate the node before it is rendered. Before we do anything we check
Is this the right type of node?
Is our op = "load"?
Is the value our default value?
What we then do is pass the existing default value through the t() function. This will make the default string available to i18n translation table, and you can then use the normal way of translating strings.
*DISCLAIMER*
I have not tested this myself in production. Im not entirely sure what the effects will be. You probably want to think this through before implementing it, and you probably want to put some features in to look up the default values from the DB incase they are changed in the CCK UI.
Hope this helps - or possibly shakes a hint of a solution to your problem!
When a user login , the user will be redirect to a user profile page, which has a My account field set.
the field set has 2 fields, "Username: ", "Email address:". those 2 fields are generated by drupal.
those 2 field contained in a form which has a id ("user_profile_form") . I want to change the order of those 2 fields.
I have tried to intercept 'user_profile_form' , inside hook_form_alter.
code as follow:
$form['account']['name']['#weight'] = 1;
but that did not success, drupal did not even rendering the 'name' field, so no username: showed on browser.
What you did is absolutely correct, and probably did work. You can change the weight of the fields with the method described above.
The username field is not always rendered. The reason is that a persmission is required: change own username. If that perm is not set, you wont be allowed to alter you username and the field wont be shown.
Info on debugging.
Your info alone is not quite enough to debug. From what you describe, you are doing the right thing, but other modules could be making things a bit tricky for you. The devel module is quite good when it comes to debugging, ti defines two functions I use a lot when debugging:
dpm() pretty prints the variable to the message area using krumo.
dd() Prints / saves a variable to a log file. Useful when you can't view messages on the screen.
I would suggest that you look at the $form variable before and after you alter it.
Things that could make it go wrong:
Did you remember to pass the $form variable by reference using the & notation?
Is another module altering your form after you?
Are you checking for the correct form id, so you alter the correct form?
These are some pointers, before you bring more info, all I can do is guess to what your problem exactly can be. I did something like this a few days ago so I know what you describe shouldn't be a problem.