Drupal 7 returning theme() to page - php

I am developing a module and I need to call different .tpl.php layouts from the theme directory per page.So what I currently have is:
return theme('adverts_index', array('adverts' => $advertsThemeArray));
That is what I return in one of the page callback functions. Now this works perfectly without a problem but in another page callback function within the module I attempt the same thing with a different theme hook but it does not call the layout specified by the theme hook.
$r = theme('adverts_view_advert', array(
'advert' => array(
'id' => $advert->aid,
'seller' => $advertiser,
'more_from_user' => array(),
'year' => $advert->year,
'condition' => $advert->condition,
'region' => $advert->region,
'city' => $advert->city,
'content' => $advert->description,
'bids' => $allBidsArray,
),
)
);
return $r;
The page is not complete blank, the main page.tpl.php which is used on every page which contains the header and what not does still display. Just the template I am trying to call to its contents is not displaying.
This is my hook_theme:
function adverts_theme() {
$theme = array();
$theme['adverts_index'] = array(
'template' => 'adverts-index',
'variables' => array(
'advert' => array(
'title' => null,
'formatted_price' => null,
'image' => null,
'permalink' => null,
'formatted_date' => null,
'description' => null,
),
),
);
$theme['adverts_view_advert'] = array(
'template' => 'adverts-single',
'variables' => array(
'advert' => array(
'id' => null,
'seller' => null,
'more_from_user' => null,
'year' => null,
'condition' => null,
'region' => null,
'city' => null,
'content' => null,
'bids' => null,
),
),
);
return $theme;
}
Any help would be highly appreciated!

arguments changed to variables in Drupal 7, and since either variables or render element is required the registry won't be picking up your theme.
Make the two changes in the hook_theme() implementation, clear the caches, and you should be good to go.

Related

How to automatically edit page configuration and add a template from extension controller?

I am writing an Extbase Extension that automatically creates pages.
So far I have managed to add the pages, and they show up in the page tree. I have also got their page ids saved in variables.
$data = array(
'pages' => array(
'NEW_1' => array(
'pid' => '-1',
'title' => 'Page1',
),
'NEW_2' => array(
'pid' => 'NEW_1',
'title' => 'Page2',
),
'NEW_3' => array(
'pid' => 'NEW_1',
'title' => 'Page3',
),
'NEW_4' => array(
'pid' => 'NEW_1',
'title' => 'Page4',
),
)
);
$tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
$tce->stripslashes_values = 0;
$dataHandler->reverseOrder = 0;
$tce->start($data, []);
$tce->process_datamap();
\TYPO3\CMS\Backend\Utility\BackendUtility::setUpdateSignal('updatePageTree');
$tce->clear_cacheCmd('pages');
$Page1id=$tce->substNEWwithIDs['NEW_1'];
$Page2id=$tce->substNEWwithIDs['NEW_2'];
$Page3id=$tce->substNEWwithIDs['NEW_3'];
$Page4id=$tce->substNEWwithIDs['NEW_4'];
Now I want to automatically add a Fluid-Template to these pages but I can't figure out how to do that without manually adding one in the backend. Does someone know how to do that?
Thank you in advance for your help.
As fluid templates are not assigned to pages immediately you need to set a layout which selects a template. You have multiple fields in a pages record. The most common fields probably are backend_layout and backend_layout_next_level. You also could use the field pagelayout but I prefer backend_layout as it has a nice inheritance mode, where you could except single pages.
Solutions could be found in the web or here on StackOverflow.
If your fluid template is selected with one of these fields you could set this field like any other field while creating the new records.
$data = array(
'pages' => array(
'NEW_1' => array(
'pid' => '-1',
'title' => 'Page1',
'backend_layout' => 'SpecialPageLayout',
),
'NEW_2' => array(
'pid' => 'NEW_1',
'title' => 'Page2',
'backend_layout' => 'SpecialPageLayout',
),
:

SugarCRM On-Demand Convert TextField to DropDown

A similar question was posted here before but it only applied to an On-Site SugarCRM instance (it was also a long time ago).
In my case I would like to change the "Billing Country" field from 'TextField' to 'DropDdown' on an On-Demand instance where I don't have access to the config files. How can I do this?
So far I have tried different PHP files using the Module Loader but it's not working either.
Please find my PHP script below:
<?php
$manifest = array(
'acceptable_sugar_flavors' => array('CE', 'PRO', 'CORP', 'ENT', 'ULT'),
'acceptable_sugar_versions' => array(
'exact_matches' => array(),
'regex_matches' => array(
0 => '6\\.5\\.(.*?)',
1 => '6\\.7\\.(.*?)',
2 => '7\\.2\\.(.*?)',
3 => '7\\.2\\.(.*?)\\.(.*?)',
4 => '7\\.5\\.(.*?)\\.(.*?)',
5 => '7\\.6\\.(.*?)\\.(.*?)'
),
),
'name' => 'Textfield to Dropdown',
'description' => 'Converting Billing Country textfield to Dropdown Field',
'author' => 'myname',
'icon' => '',
'is_uninstallable' => true,
'published_date' => '2015-01-09 19:00:00',
'type' => 'module',
'version' => '1.0',
);
$dictionary['Account']['fields']['billing_address_country']['type'] = 'enum';
$dictionary['Account']['fields']['billing_address_country']['ext1'] = 'countries_dom';
The module loads fine but there's no change after a Quick Repair. I have tried with the following variables, as well:
$dictionary['Account']['fields']['billing_address_country']['isDropDown']=yes;
and...
$dictionary['Account']['fields']['billing_address_country']['Options']='countries_dom';
Can anyone tell me what I am doing wrong? I'd appreciate any help or guidance you have. Please advise.
Cheers,
H
P.S. I am using SugarCRM On-Demand Version 7.6.1
You need to create the manifest.php file and have that point to the file(s) you want to copy/upload for customization:
manifest.php:
<?php
$manifest = array(
'built_in_version' => '7.6.1.0',
'acceptable_sugar_versions' =>
array(
0 => '',
),
'acceptable_sugar_flavors' =>
array(
0 => 'PRO',
1 => 'CORP',
2 => 'ENT',
3 => 'ULT',
),
'readme' => '',
'key' => 'SO',
'author' => 'Eric',
'description' => 'Convert Accounts Billing Country TextField to Enum',
'icon' => '',
'is_uninstallable' => true,
'name' => 'TextFieldToDropdownPkg',
'published_date' => '2016-01-10 03:01:01',
'type' => 'module',
'version' => 1452378413,
'remove_tables' => 'prompt',
);
$installdefs = array(
'id' => 'TextFieldToDropdownPkg',
'copy' => array(
0 => array(
'from' => '<basepath>/custom/Extension/modules/Accounts/Ext/Vardefs/custom_billing_address_country.php',
'to' => 'custom/Extension/modules/Accounts/Ext/Vardefs/custom_billing_address_country.php',
),
),
);
Then create:
custom/Extension/modules/Accounts/Ext/Vardefs/custom_billing_address_country.php
<?php
$dictionary['Account']['fields']['billing_address_country']['comments']='Country for primary address';
$dictionary['Account']['fields']['billing_address_country']['group']='primary_address';
$dictionary['Account']['fields']['billing_address_country']['options']='countries_dom';
$dictionary['Account']['fields']['billing_address_country']['type']='enum';
ZIP those files up and use Module Loader.

HelperList status action with ajax

I'm in charge of creating a small payment module. The configuration has to be managed with a simple CRUD, and I use the HelperList class to display a table with the records stored in the database.
One of the tables database structure is similar to this
'CREATE TABLE IF NOT EXISTS '._DB_PREFIX_.'MODULE_ITEM
(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`active` VARCHAR(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;'
So, the list_fields value is like this
array(
'id' => array(
title' => $this->l('Id'),
//'width' => 140,
'type' => 'text',
'align' => 'center'
),
'name' => array(
'title' => $this->l('Name'),
//'width' => 140,
'type' => 'text',
'align' => 'center'
),
'active' => array(
'title' => $this->l('Status'),
//'width' => 140,
'active' => 'statusItem',
'type' => 'boolean',
'align' => 'center',
'ajax'=> true
)
);
As I intend to enable or disable the item via a button I use the 'active' and 'ajax' options for this specific field, and when rendered in the module configuration page the link generated for the column in question is something like: index.php?controller=AdminModules&configure=Example&item_id=4&statusItem&action=statusItem&ajax=1&(...). Please notice that statusItem is the name of the action.
On the other hand, I wrote this function in the module main file, which should change the item status.
public function ajaxProcessStatusItem()
{
$id=(int)Tools::getValue('item_id');
$value=(int) Db::getInstance()->executeS($this->createSelectQuery('module_item','item_id',$id))[active];
Db::getInstance()->update('module_item', array('active' => !$value), 'item_id='.$id);
die();
}
I've been using this article of the official documentation to create the list, but no matter what name I use ('ajaxProcess', 'ajaxProcessSatusItem', 'statusItem', and every caps variation I could think of) all I get is a blank page in response, and no change in the status. I had a look at the source code and there is no comment in the HelperList class regarding how the function should be called.
Any help will be appreciated.
If you use ObjectModel class for your data object, you can autmatically generate toggle button just by adding one line:
AdminProductTabController.php or when defining fields somwehre else
and calling HelperList->generate()
'active' => array(
'title' => 'Active',
'active' => 'status',
'filter_key' => '!active',
'type' => 'bool',
'width' => 'auto',
'orderby' => false,
'search' => false,
)
Line 'active' => 'status', doesn't refer to any field names. Add this line to your list definition (if you're defining list field properties in Admin{YourObjectModel}Controller or calling HelperList from somewhere else).
An excerpt from my ObjectModel:
ProductTab.php
class ProductTab extends ObjectModel {
.......
public static $definition = array(
..........
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool',),
I looked up my code and I noticed that I actually called a special processing function:
AdminProductTabController.php
public function initProcess()
{
$id_product_tab = (int)Tools::getValue('id_product_tab');
$product_tab = new ProductTab($id_product_tab);
$isStatusAction = Tools::getIsset('status'.$this->table);
if ($isStatusAction)
{
$product_tab->toggleStatus();
Tools::redirectAdmin($this->href_back);
}
}
Hope this will help you out.

Creating new node type with custom field makes page for adding the node without any fields in Drupal 7

I'm trying to make a simple custom field "field_book_year" for the new node type "synopsis_book".
I have wrote in .install file:
function synopsis_install() {
node_types_rebuild();
$types = node_type_get_types();
// In Drupal 7 you must explicitly add a "body" field when you create a custom content type,
// thus the call to node_add_body_field($types['newsletter']);.
node_add_body_field($types['synopsis_book']);
$body_instance = field_info_instance('node', 'body', 'synopsis_book');
$body_instance['type'] = 'text';
field_update_instance($body_instance);
field_create_field(array(
'field_name' => 'field_book_year',
'label' => t('Company posting the job listing'),
'type' => 'text',
'translatable' => TRUE,
)
);
field_create_instance(array(
'field_name' => 'field_book_year',
'label' => t('Company posting the job listing'),
'entity_type' => 'node',
'bundle' => 'synopsis_book',
'type' => 'text',
'widget' => array(
'type' => 'text_textfield',
),
'display' => array(
'example_node_list' => array(
'label' => t('Company posting the job listing'),
'type' => 'text',
),
),
'description' => 'Begin Date',
)
);
}
Then, I have in .module these functions:
function synopsis_node_info()
{$types = node_type_get_types();print_r($types);
return array(
'synopsis_author' => array('name' => t('Author'), 'base' => 'synopsis_author', 'description' => t('Author description, biography etc.'),
'has_body' => true, 'has_title' => true, /*'min_word_count' => 11,*/ 'help' => t('Enter blah-blah-blah'),
'title_label' => t('Author full name')
),
'synopsis_book' => array('name' => t('Book'), 'base' => 'synopsis_book', 'description' => t('Book description, synopsis etc.'),
'has_body' => true, 'has_title' => true, /*'min_word_count' => 11,*/ 'help' => t('Enter blah-blah-blah'),
'title_label' => t('Book title')
),
);
}
/**
* Implement hook_form() with the standard default form.
*/
function synopsis_book_form($node, $form_state) {
return node_content_form($node, $form_state);
}
/**
* Implements hook_validate().
*/
function synopsis_book_validate($node)
{
// Enforce a minimum character count of 2 on company names.
if (isset($node->field_book_year) &&
strlen($node->field_book_year['und'][0]['value']) < 2
) {
form_set_error('job_post_company',
t('The company name is too short. It must be atleast 2 characters.'),
$limit_validation_errors = NULL);
}
}
Not matter all I've written, the page of adding the Book looks like this! No 'field_book_year' field. There is even no body field
What am I doing wrong? Thank you.
I think the problem you're having is that hook_install is run when your module is installed, before the node type is created. So essentially you're trying to attach a field to a non-existant node type at that point.
The way I've always done this is to create the node type in hook_install before trying to attach the field (this is an example from a module that provides a testimonial content type):
// Make sure a testimonial content type doesn't already exist
if (!in_array('testimonial', node_type_get_names())) {
$type = array(
'type' => 'testimonial',
'name' => st('Testimonial'),
'base' => 'node_content',
'description' => st("Use <em>basic pages</em> for your static content, such as an 'About us' page."),
'custom' => 1,
'modified' => 1,
'locked' => 0,
'title_label' => 'Customer / Client Name'
);
$type = node_type_set_defaults($type);
node_type_save($type);
node_add_body_field($type);
}
// See if the testimonial date field exists
if (!field_info_field('field_testimonial_date')) {
field_create_field(array(
// Field info here
));
}
// If the date field is not attached, attach it
if (!field_info_instance('node', 'field_testimonial_date', 'testimonial')) {
field_create_instance(array(
// Field instance info here
));
}
If you look in the standard installation profile install file you'll see this is actually how the Drupal core performs this task, so I would expect it's the 'correct' way to do it.

How do I save custom node types in Drupal 7

I've created a custom node type in Drupal 7, using the hook_node_info method in the install file:
// declare the new node type
function foo_node_info ( ) {
return array(
'foo' => array(
'name' => t('Foo entry'),
'base' => 'node_content',
'description' => t('For use to store foo entries.'),
));
} // END function foo_node_info
and I'm trying to save that type in the module file using the following code:
// INSERT the stuff
node_save(node_submit((object)array(
'type' => 'foo',
'is_new' => true,
'uid' => 1,
'title' => 'Title, blah blah blah',
'url' => 'url here, just pretend',
'body' => '<p>test</p>',
)));
My issue, is that the url, and body fields aren't saving. Any idea what I'm doing wrong?
So, after a ton of digging, it turns out that the way I was entering the custom fields in the node_save was wrong. The node_save needs to look like the following:
node_save(node_submit((object)array(
'type' => 'foo',
'is_new' => true,
'uid' => 1,
'title' => 'the title',
'url' => array(
'und' => array(array(
'summary' => '',
'value' => 'url value',
'format' => 2,
))),
'body' => array(
'und' => array(array(
'summary' => '',
'value' => 'the body goes here',
'format' => 2,
))),
)));
Notice that for the custom fields, the array structure has to match what was previously going on with CCK (pretty much exactly). The first key in the array describing the field value is the language for the content.
I've used 'und' here only because that's what I saw going into the database when entering the data through a form.

Categories