Translating fields in node.tpl.php file with drupal 7 - templates

I have a drupal English/French website.
I have a custom content type called 'ad' with all sort of fields.
I have created the file 'node--ad.tpl.php' in my theme directory
to customize the display of the 'ad' content.
I use pixture reloaded theme and DRUPAL 7.
I am trying to translate fields (both labels and values)
by using 'field translation' module.
Field translation works when I CREATE or MODIFY a content via admin.
However, when I DISPLAY a content of type 'ad', fields are not translated.
This is because drupal calls to 'node--ad.tpl.php' and the translation
module is probably not invoked.
When deleting 'node--ad.tpl.php', drupal calls the default node.tpl.php
with a similar results.
Any help would be appreciated,
Thanks in advanced,
Notes :
1 - I correctly activated every dependencies for the module.
2 - User interface translation correctly works.

OK
I found out myself how to do this.
I give the solution here.
It may help for others :
First, translate field labels and values in Configuration > Regional and language > Translate > Import. Do not import values as Fields but as User Interface. Do not specify
an URL (only msgstr and msgid).
Now, you need to add your t() function in the node.tpl.php for the translation to be effective. So :
In the node.tpl.php file, if you want to translate field label, write this :
$content['field_my_field']['#title'] = t($content['field_my_field']['#title']);
Then to translate field value do this :
$content['field_my_field']["#items"][0]['value'] = t($content['field_my_field']["#items"][0]['value']);
You can now render your field : print render($content['field_my_field']);

Related

Flask WTForms - option_widget for SelectMultipleField?

I have a field in my Flask WTForm which uses a SelectMultipleField. I'm using a frontend library which renders the choices fine from the db and adds them to the input field as tags when I select them (like the tags do on this website when creating a question!).
However the data is not being saved, form.field.data and request.form.getlist('field') etc all show None. If I add option_widget=widgets.CheckboxInput to the SelectMultipleField, I can select and save data.
So what I'm wondering is, do I need make a custom field or widget in order for the form to use the selected options as the form data (for example, instead of checking if the field has been checked, it checks if it's in the input field). Going a bit mad reading all the documentation so grateful for a hint in the right direction! Code below:
field = SelectMultipleField(
"fieldname",
validators=[Optional()],
widget=widgets.ListWidget(prefix_label=False),
# option_widget=CheckboxInput(),
)
please try this way
from flask_appbuilder.fieldwidgets import Select2ManyWidget
"users": QuerySelectMultipleField(
_("group.user"),
query_factory=lambda: db.session.query(User),
widget=Select2ManyWidget(),
default=[],
),
The result will look like the image below

Save the dynamically populated value on dropdown

I'm using wagtail CMS for Django, I want to add a dynamically populated value for a dropdown and save it on the Page model, this is my code:
class MyPage(Page):
domain = CharField(max_length=10, choices=MY_CHOICES)
subdomain = CharField(max_length=10, choices=[('', '------')]
I've got some frontend logic to populate dynamically the options for subdomain, but after I hit save I got: The page could not be created due to validation errors And in the subdomain field: Select a valid choice. [my value] is not one of the available choices.
I can't use ForeignKey to populate subdomain because it depends from an external API service that we're using.
I tried to use a custom field that inherits from CharField with no success, it looks it executes validate method only for the domain field.
If you use the choices argument, you have to predefine the list of possible values. Read the relevant part of the docs (last two paragraphs of that section).
You could omit the choices argument from the model field definition and only render a HTML select tag in the frontend (which is then filled with options dynamically, like you explained).
You could also look into changing the default widget of the CharField to a select tag, like this answer and this part of the docs show.

How to make a NameValueCollection list editable in GlassMapper

I am trying to make a NameValueList collection editable with GlassMapper and I don't seem to be able to get to the bottom of this.
We have a list of validations that can be attached to a field and I would like to have the validation message editable in ExperienceEditor.
The collection is pre-processed when GlassMapper is retrieving the item:
Validations = glassItem.GetValidations();
#foreach(Validation validation in Model.Validations)
{
<div id="#validation.Identifier" ng-message="#validation.AngularKey" ng-cloak class="mtg-validation-msg">
#Html.Glass().Editable(validation, e => e.ErrorMessage)
</div>
}
Error that I am getting:
Failed item resolve - You cannot save a class that does not contain a property that represents the item ID. Ensure that at least one property has been marked to contain the Sitecore ID. Type: MyAssembly.Models.Validation
It is not possible to directly edit certain types of complex fields in the Experience Editor, such as Treelist, Multilist or Name Value Collection.
Instead, you should set up and use an Edit Frame. This will pop up a modal dialog allowing you to edit the field, it is not inline but means you do not need to leave the Experience Editor. This is the recommended approach to this problem.
Since you are using Glass Mapper, since version 4 you can declare Edit Frames all directly from code and now have to declare/set them up in the Core database first.
#if (Sitecore.Context.PageMode.IsExperienceEditor)
{
using (Html.Glass().BeginEditFrame(Model, "Edit", x => x.Validations))
{
<div>Edit Validations</div>
}
}
You might be interested in this blog post I wrote about adding a wrapper around the Edit Frame to make the UX more friendly.

Add hidden field at runtime

In Sitecore WFFM(Web Form for Marketers) - Can we add hidden field at runtime - i.e. on Submit Action and assign some value to the hidden field.
I also want to reuse this value if there is an exception on the form and user resubmits the form.
Product Details - 7.2 rev. 141226 , Web Forms for Marketers 2.4 rev.140117
As the very simple, you may create hidden field like this (of course, you may provide alternative and more complex bespoke behavior for your hidden field):
public class HiddenField : SingleLineText
{
protected override void DoRender(System.Web.UI.HtmlTextWriter writer)
{
this.textbox.Parent.Parent.Visible = false;
base.DoRender(writer);
}
}
With the next step you need to register that field with WFFM. In order to do that just create an item for your new field:
/sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types/Custom/Hidden Field
Within that item you specify assembly and class name for that hidden field, as is very common way to Sitecore.
So far, so good. Now you can use your hidden field as others, including adding them programmatically.
P.S. Unfortunately, I do not have an example of code for adding fields programmatically at the moment. However you can try finding that yourself with Reflector, dotPeek or any other disassembling tool. Library should be in your \bin folder already, called Sitecore.Forms.Core.dll

Customising specific fields in Django Admin change form

I have a couple of fields in my model that to which I wish to add a link that will allow the user to search for names/files (external from the application's database).
So what I would like is:
Field name: [text box] - LINK
Is there a straightforward django way of achieving this?
Cheers.
You need to change the widget that the form field uses to display the models information. You basically add some html after the input to link to where you want.
Here's some code I put together to create a widget that displays how many characters are left for a CharacterField so it's similar to what you are looking to do:
https://github.com/pastylegs/django-widget-charsleft/blob/master/widget_charsleft/widgets.py