I am trying to translate a Tab and a TextField but it is not translating at the moment. Current setup as per below:
Locale is set in _config.php - I have flushed.
i18n::set_locale('de_DE');
mysite/lang/de.yml
de:
Page:
FULLNAME: 'Testing this'
CONTACTDETAILS: 'Root.Trying to change to this text'
Page.php
<?php
class Page extends SiteTree {
private static $db = array(
'FullName' => 'Varchar(255)'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldsToTab(_t('Page.CONTACTDETAILS', 'Root.ContactDetails'), array(
TextField::create('FullName', _t('Page.FULLNAME', 'Full Name'))
));
return $fields;
}
}
But the text is not being translated it just shows in English. What am I doing wrong?
The CMS uses the currently logged in user's Locale field for translations afaik. You can change a user's locale to de_DE (whereupon your translations should work) by going to Security, selecting the user, and changing Interface Language to German (Germany).
It's also possible to set the locale inside your getCMSFields if you only want the fields in there to get translated:
public function getCMSFields()
{
$oldLocale = i18n::get_locale();
i18n::set_locale('de_DE');
$fields = parent::getCMSFields();
$fields->addFieldsToTab(_t('Page.CONTACTDETAILS', 'Root.ContactDetails'), array(
TextField::create('FullName', _t('Page.FULLNAME', 'Full Name'))
));
i18n::set_locale($oldLocale);
return $fields;
}
The Locale of new users created through the CMS will be set based on the Locale of the user that is creating them.
Related
In Drupal 7, is it possible to show a result of a DB query on each users' respective profile page, in some table? I need to do this programmatically within my existing module. So the input to the query would be the ID of a user whose profile is currently being viewed.
Only to show the queried data - no administration, no edits, nothing else.
something along the lines of.. (image)
Also the block or field or whatever would make this possible needs to be configurable through the _permission() hook as to who can or cannot view it.
I thought since this is basically just a query with no extra custom stuff there would be an easy way via the Drupal API.
you can create custom block for that and view it in current user profile
/**
* Implements hook_block_info().
*/
function custom_block_block_info() {
$blocks = array();
$blocks['my_block'] = array(
'info' => t('My Custom Block'),
'status' => TRUE,
'region' => 'Content',
'visibility' => BLOCK_VISIBILITY_LISTED,
'pages' => 'user/*',
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function custom_block_view($delta = '')
{
// The $delta parameter tells us which block is being requested.
switch ($delta)
{
case 'my_block':
// Create your block content here
$block['subject'] = t('This is just a test block created programatically');
$block['content'] = _user_detail_list();
break;
}
return $block;
}
/**
* Implements costome code we want to print().
*/
function _user_detail_list(){
//enter your query and output in some variable
$value = "<p>User Detail</p>"
return $value;
}
Note :- Here profile is extended with new block
There will be some coding to get what you want, but if you just want style/show the data that's already available with the "user" object then #1 below will do it.
Easy way(#1):
1. Create a view and choose the "user" info that you need shown and give it a path. Then in your sub-theme use the correct template -see the code snippets.
https://www.drupal.org/forum/support/post-installation/2011-04-04/modify-the-default-profile-pagelayout
other ways:
use the user-profile.tpl.php see
https://api.drupal.org/api/drupal/modules%21user%21user-profile.tpl.php/7.x
in your module, you need to call and reach out to the hook_user_view.
https://api.drupal.org/api/drupal/modules%21user%21user.api.php/function/hook_user_view/7.x
Here you fetch user profile data from database then follow it
function modulename_menu() {
$items['user-data'] = array(
'title' => 'User data',
'page callback' => 'user_data',
'access callback' => ('user_is_logged_in'),
'#type' => MENU_NORMAL_ITEM,
);
return $items;
}
function user_data(){
global $user;
$user_fields = user_load($user->uid);
$output = "" //return those $user_fields values into table using theme('table',header,rows)
return $output;
}
https://www.drupal.org/node/156863 (for create table view)
i.e
global $user;
$user_fields = user_load($user->uid);
$firstname = $user_fields->field_firstname['und']['0']['value'];
$lastname = $user_fields->field_lastname['und']['0']['value'];
I have a custom field that extends the ImageField to add a checkbox to the image, similarly to how the image field contains a text field for title and alt information. I can get the checkbox to appear and save values as they show on the formatter page ok, but I cannot figure out how to get the default value for the checkbox. My code initializes the checkbox in formElement function:
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$field_settings=$this->getFieldSettings();
// Setup how the show_image field is shown on the node edit form.
$element['#show_image'] = $field_settings["show_image"];
return $element;
}
And then creates the checkbox field when the file is uploaded and the process callback function is called:
/**
* {#inheritdoc}
*
* This is a callback from formElement. For example, this could be triggered from an ajax call.
*/
public static function process($element, FormStateInterface $form_state, $form) {
$item = $element['#value'];
$item['fids'] = $element['fids']['#value'];
$element["show_image"] = array(
'#default_value' => $element['#show_image'],
'#type' => 'checkbox',
'#title' => t('Show image on website'),
'#description' => t('This may be from an external source. Show on the website?'),
'#weight' => 10,
'#access' => (bool) $item['fids']
);
return parent::process($element, $form_state, $form);
}
After changing the #default_value key, I can't seem to have the form both display the default setting of "checked" on new uploads, and show the correct value that is saved in previously saved nodes. For example, if I set the
#default_value=>$element['#show_image']
I always get the default value, no matter what the user saves. If I set it like this:
isset($item['show_image']) ? $item['show_image'] : $element['#show_image']
It always shows the saved value, and never the default, even on new uploads. So my question is, how do I show both the default value for new uploads and the saved value for updates?
I've run into a very bizarre issue while creating 2 TreeDropdownFields for a DataObject. For some reason, only 1 of the 2 TreeDropdownFields render correctly in the SilverStripe admin. The other doesn't render as a TreeDropdownField at all but just as a label:
Here is the code:
class HomeBanner extends DataObject {
public static $db = array(
'SortOrder' => 'Int',
'Title' => 'Varchar'
);
public static $has_one = array(
'Image' => 'Image',
'SecondaryImage' => 'Image',
'FirstLink' => 'SiteTree',
'SecondLink' => 'SiteTree'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeFieldFromTab('Root.Main', 'PageID');
$fields->removeFieldFromTab('Root.Main', 'SortOrder');
$fields->addFieldToTab('Root.Main', new TreeDropdownField('FirstLinkID', 'First Link', 'SiteTree'));
$fields->addFieldToTab('Root.Main', new TreeDropdownField('SecondLinkID', 'Second Link', 'SiteTree'));
return $fields;
}
public static $summary_fields = array(
'ID' => 'ID',
'Title' => 'Title',
'Thumbnail' => 'Thumbnail'
);
public function getThumbnail() {
return $this->Image()->CMSThumbnail();
}
}
Here is what I have tried so far:
running dev/build/?flush=true
running ?flush=all and ?flush=1
logging out and logging back in after the dev/build + flushes
logging into the admin in another browser (I typically use Chrome but
logged into the site's admin on FireFox and saw the same problem)
The error logs report nothing -- they're clear
There are no errors in the console for Chrome's dev tools
Adding a third TreeDropdownField will allow the first 2 to render
properly but the third one will just show a label instead of a
TreeDropdownField
This format works, but doesn't save whatever is selected--it clears your choice as soon as you leave the page. Also, it deletes all that was saved already in the admin unless I remove it. I can't make changes or else the items saved get removed.):
$fields->addFieldToTab('Root.Main', new TreeDropdownField('SecondLink', 'Second Link', 'SiteTree', 'ID'));
Does anyone have any ideas as to why this could be happening? It doesn't seem to make sense that you can't have multiple TreeDropdownFields.
Reposting as this turned out to be the answer:
The name “HomeBanner” suggests to me that there should also be a has_one pointing back to HomePage or similar? The cause of this is probably that SilverStripe is automatically trying to set one of the has_one relations to point back to the page that the banner belongs to.
Similar conflicts can also happen when using code like this:
class Page extends SiteTree {
private static $has_many = [
'Banners' => 'Banner'
];
}
class Banner extends DataObject {
private static $has_one = [
'Page' => 'Page',
'LinkedPage' => 'Page'
];
}
As SilverStripe doesn't know whether it should use PageID or LinkedPageID to auto-populate that side of the has_many relation (GridField will try to automatically assign the correct has_one ID).
In these cases, you can use dot-notation to distinguish between them - you’d change it to $has_many = ['Banners' => 'Banner.Page'];. See https://docs.silverstripe.org/en/3/developer_guides/model/relations/#has-many for more info.
Is there a way to create a formatter for drupal email field to decrypt when viewed in a view table.
I have tried to create a formatter for the same using the below code
function MYMODULE_field_formatter_info() {
return array(
'views_decrypt_field' => array(
'label' => t('Decrypt this field'),
'field types' => array('textfield'),
),
);
}
function MYMODULE_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'views_decrypt_field') {
dsm($items);
}
return $element;
}
when I run this code, other fields with "textfield" type shows this formatter.
I am trying to create a generic formatter for all "text" type fields so that if they are encrypted then I can use this formatter in the view to decrypt the same.
N.B: The email field is not showing any formatter dropdown in the field settings in the VIEW.
You need to use all function listing in this page : https://api.drupal.org/api/drupal/modules!field!field.api.php/group/field_formatter/7.x
hook_field_formatter_info => Expose Field API formatter types.
hook_field_formatter_info_alter => Perform alterations on Field API formatter types.
hook_field_formatter_prepare_view => Allow formatters to load information for field values being displayed.
hook_field_formatter_view => Build a renderable array for a field value.
I have installed drupal 7 and have been trying to create a custom form. The below code which am trying has been taken from http://drupal.org/node/717722 and I have not made any changes except for .info file.
here is the my_module.info
name = My module
description = Module for form api tutorial
core = 7.x
Below is the my_module.module
<?php
/**
* This function defines the URL to the page created etc.
* See http://api.drupal.org/api/function/hook_menu/6
*/
function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
'title' => t('My form'),
'page callback' => 'my_module_form',
'access arguments' => array('access content'),
'description' => t('My form'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* This function gets called in the browser address bar for:
* "http://yourhost/my_module/form" or
* "http://yourhost/?q=my_module/form". It will generate
* a page with this form on it.
*/
function my_module_form() {
// This form calls the form builder function via the
// drupal_get_form() function which takes the name of this form builder
// function as an argument. It returns the results to display the form.
return drupal_get_form('my_module_my_form');
}
/**
* This function is called the "form builder". It builds the form.
* Notice, it takes one argument, the $form_state
*/
function my_module_my_form($form_state) {
// This is the first form element. It's a textfield with a label, "Name"
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
);
return $form;
}
?>
I have placed these two files in a *my_module* folder and placed it in sites/all/modules
After that, I enabled the module from the modules page without any errors or warnings.
Now, when I try to access this for using the url, localhost/d7/?q=my_module/form
I get a "Page not found " error..!! Why..?? What am I missing..?
Its not only for this module but also for this examples for developers module http://drupal.org/project/examples. It shows the same error.
You should write:
$items['my_module']
Where my_module is module name.
And you need to create page-my_module_my_form.tpl.php file at
sites/all/theme/your_theme/template/page-my_module_my_form.tpl.php
and in this file add code like this:
<?php
if (isset($form['submission_info']) || isset($form['navigation'])) {
print drupal_render($form['navigation']);
print drupal_render($form['submission_info']);
}
print drupal_render($form['submitted']);
?>
<?php print drupal_render_children($form); ?>
and try to run with
localhost/d7/my_module
I hope this will be useful to you
I know this is late, but I do believe that you need to have the $form variable passed into your form, like so : function my_module_my_form($form_state, $form)... That way you actually have a form variable to house your form data.