How to wrap H2 tags on field in a node in Drupal 7 - drupal-7

I want to wrap h2 tags around a 'content type' text field (the field "product_title"), as a default setting, that the user cannot change.
I have tried to add this snippet in the file template.php in my theme:
function mytheme_preprocess_field(&$vars) {
if($vars['element']['#field_name'] == 'field_produktnamn'){
$vars['items']['0']['#markup'] = '<h2>This works</h2>';
}
}
But I can't figure out how to keep the original field content. I only want to add h2 tags, not replace the text content.
Or maybe I should go about this another way?

try using this instead:
$vars['items']['0']['#prefix'] = '<h2>';
$vars['items']['0']['#suffix'] = '</h2>';

Related

Agile Toolkit manipulating Grid column content

Just started using ATK4 and appreciating it very much so far, but not sure how to do this...
What I am trying to accomplish:
I am outputting a query's results to a grid, one of the fields is 'status', the data will either be '-1' or '1'.
Instead of outputting -1 or 1 to the column, how do I output an HTML snippet (or whatever I need to to get what I want) instead that shows a different icon for each value?
In short:
In column 'status':
if the value is -1, display iconDown.gif;
if the value is 1, display iconUp.gif
Code so far:
class page_showlist extends Page {
function init(){
parent::init();
$q=$this->api->db->dsql();
$q->table('remote_system')
->join('customers.id','customer_id')
->field('customer_id')
->field('ip')
->field('nickname')
->field('name','customers')
->field('status')
;
$grid = $this->add('Grid');
$grid->addColumn('text','status')->makeSortable();
$grid->addColumn('text','name')->makeSortable();
$grid->addColumn('text','ip');
$grid->addColumn('text','nickname');
$grid->addButton('Reload Grid')->js('click',$grid->js()->reload());
$grid->addQuickSearch(array('name'));
$grid->setSource( $q );
}
}
Any pointers/tips?
To add column with icons in Grid you can use custom template.
In one of my projects I do like this:
$url = $this->api->pm->base_url . $this->api->locateURL('template', 'images/');
$grid->addColumn('template', 'type', false)
->setTemplate('<img src="' . $url . 'icon_object_<?$type?>.png">');
It'll use model field named type (in your case use status) and show icons in that column. Icon source URL is generated dynamically and it'll search for image files in your template/images directory named icon_object_XXX.png where XXX value will be taken from field type value.
In my case type is like this: array('building','apartment','land','garage') etc.
And one more thing - you should start using Models whenever possible! That way you'll ease your life later when your project becomes bigger. Also can have extra security (conditions, etc.) with them.

drupal 7 adding custom cck field in html.tpl.php head

I need to add a custom base href, I have a organic groups multisite setup. I have a field field_site_url which renders the URL correctly how would I get this to print
I did this but it doesnt work
base href="what do i put here" target="_blank"
Any ideas please
Thanks
I get CCK / fields data into html.tpl.php in d7 by using a preprocess function in template.php:
function THEMENAME_preprocess_html(&$variables, $hook) {
$node = menu_get_object();
if($node)
{
$variables['myfield'] = $node->field_myfield['und'][0]['value'];
}
}
This sets up a $myfield variable you can output in html.tpl.php :
echo $myfield;

How can add new field to an event with dhtmlx scheduler?

I want to add new fields or propierties to an calendar event. How can I do it?
Thanks!
You need to
add new section to lightbox form
http://docs.dhtmlx.com/doku.php?id=dhtmlxscheduler:details_form
for new section of lightbox, set map_to value as name of new field
on server side add extra field to the list of fields in render command
This is how you would create a simple href link inside an event:
1) Go to the Event Configuration Console and create a custom field, under the section that says 'CUSTOM FIELD'. Give the field a name. In this example, call it 'url'. Select its type as 'TextArea'.
2) Create another custom field, call it 'link_title'.Also a 'TextArea'. Description is optional.
SAVE
3) Go to the 'Templates' section of the event console. Under the first section (... scheduler.templates.event_text=function(start,end,event){... ) where you can input text, put ->
return "<b> "+event.text+"</b><br><br>" + event.link_title + "";
SAVE
When you go to create your event from the main calendar, you will see extra fields. Type in them, save your event, and voila. Toot Sweet.
What do you mean by calendar event?
Is it adding a new event?, then it must be done using lightbox(built-in option)
scheduler.config.lightbox.sections=[
{ name:"description", height:50, map_to:"text", type:"textarea", focus:true },
{ name:"location", height:43, map_to:"event_location", type:"textarea",
default_value:"Blackheath Avenue London,Greenwich,Greater London SE10 8XJ,UK"},
{name:"recurring", height:115, type:"recurring", map_to:"rec_type",
button:"recurring"},
{ name:"time", height:72, type:"time", map_to:"auto"}
];
each tag used in here is suppoted by the plugin,,,,in you want to have a seperate customize lightbox go to
http://docs.dhtmlx.com/scheduler/custom_details_form.html
This is the preview

Zend Form: Get array element in view script

I want to display a title and a content field for each language. So, in the form, I have:
foreach ($languages as $language)
{
// Add the title element
$title = new Zend_Form_Element_Text($language);
$title->setLabel($translate->_('News Title'))
->setBelongsTo('title');
$this->addElement($title);
// Add the content element
$content = new Zend_Form_Element_Textarea($language);
$content->setLabel($translate->_('News Content'))
->setBelongsTo('content');
$this->addElement($content);
}
If I render the form in the usual way it works perfectly:
echo $this->form;
However, I want to render each field separately to include some HTML in the middle and other jQuery stuff. My problem is that I cannot manage to access those elements. I tried
foreach ($languages as $language)
{
$this->form->getElement($language);
}
but it only renders 'content' element. Am I overriding 'title' element?
Thanks
Yes, you're overriding the Title element. The parameter you pass to new Zend_Form_Element_Text($language); ($language in your case) should be unique. Infact you can use it to identify and retrieve the element when you need.
To setup the param you can do something like this:
foreach ($languages as $language)
{
// Add the title element
$title = new Zend_Form_Element_Text('title-' . $language);
...
}

How to disable a field or make it readonly in Drupal 7

I am trying to disable couple of fields and make them readonly via hook_page_alter(). I was able to do check if user is viewing the page edit section (the form edit)
$page['content']['system_main']['#node_edit_form'] == TRUE)
then when I tried to disable couple of fields, I found that select list can be disabled by this code:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#attributes']['disabled'] = TRUE;
but if I use the following code it doesn't work:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#disabled'] = TRUE;
I also found that I can not use the same code to disable a text area field:
$page['content']['system_main']['field_my_text_area']['und']['#attributes']['disabled'] = TRUE;
The above code doesn't disable the text area, but the same code can disable the select list!
Then I tried hook_form_alter() to do the same thing, and I was able to disable fields and when I checked the rendered array from $page array, I saw that it shows:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#disabled'] = TRUE;
but when I set the same code in hook_page_alter(), it didn't work. Looks like something else will override it, I thought that hook_page_alter() is the last place to change markup.
Any idea what is the best way to disable/readonly any kind of field, inside hook_page_alter() in drupal 7?
Thank you
It works for text fields^
$form['field_secured_title']['und']['0']['value']['#attributes']['disabled'] = TRUE;
Like it said in the docs
You can use attributes :
$form['#attributes'] = array('disabled' => TRUE);

Resources