I am writing a custom module for site map in drupal 7. What I had done is below.
function escorts_sitemap_render_menu ($menu)
{
$output = "<ul>";
foreach ($menu as $item)
{
$link = $item["link"];
if ($link["hidden"])
{
continue;
}
$output .= "<li>" . $link["title"] . "</li>";
if ($item["below"])
{
$output .= sitemap_render_menu($item["below"]);
}
}
$output .= "</ul>";
return $output;
}
function escorts_sitemap_content ()
{
$output = "<h1>Escorts Sitemap</h1>";
$output .= "<ul class=\"site_map_list\">";
$output .= sitemap_render_menu(menu_tree_all_data("main-menu"));
return $output;
}
function escorts_sitemap_menu ()
{
$items = array();
$items["sitemap"] = array (
"title" => "Escorts Sitemap",
"page callback" => "escorts_sitemap_content",
"access arguments" => array("access content"),
"type" => MENU_CALLBACK);
return $items;
}
then in template.php I implemented hook_theme too, below is the code :
function escorts_theme() {
return array(
'escorts_sitemap_content' => array(
'render element' => 'content',
'template' => 'page--sitemap',
'path' => drupal_get_path('theme', 'escorts') . '/templates'
),
);
}
But it is not appearing as I have my custom template file page--sitemap.tpl.php, can any one please guide me. But now what should I write in page--sitemap.tpl.php in order to render my sitemap
Maybe You should try next:
function escorts_theme() {
return array(
'escorts_sitemap_content' => array(
'variables' => array('content' => NULL)
/**
* I would not call template like this, cause it looks like
* pattern page--[node-type]
*/
'template' => 'page--sitemap',
)
);
}
And inside of menu callback function
function escorts_sitemap_content () {
$output = "<h1>Escorts Sitemap</h1>";
$output .= "<ul class=\"site_map_list\">";
$output .= sitemap_render_menu(menu_tree_all_data("main-menu"));
return theme('escorts_sitemap_content', $output);
}
This is one of ways. Also look in preprocess functions, and Drupal theme layer at all. And maybe ready module XML sitemap will be useful. Also be careful, if file is really located there.
And in template file just paste
<?php print $content; ?>
Regards
Related
I have made a form in D7 using form API, registered some users and retrieved their registered data from database;now what i want is to add an edit and a delete link in front of every row in the retrieved table;i have done that in PHP but how to implement same in Drupal?;anybody having any idea how to do that?
My retrieval code is:
function form_data_menu() {
$items['formdata'] = array(
'title' => 'Form Data',
'page callback' => 'form_data_form',
'access callback' => TRUE,
);
return $items;
}
function form_data_form()
{
$results = db_query('SELECT * FROM {drupal}');
$header = array(t('Id'),t('Name'),t('College'),t('Education'),t('Percentage'),t('Application'));
$rows = array();
foreach($results as $result) {
$rows[] = array(
$result->id,
$result->name,
$result->college,
$result->education,
$result->percentage,
$result->application,
);
}
return theme('table',array('header'=>$header,'rows'=>$rows));
}
I can add links though but the main problem is how to run update and delete queries in D7?
Any help will be appreciated. Thank you!
Start with the example module "dbtng_example.module".
https://www.drupal.org/project/examples
This will give an example of how to set up a module to save to the database. I used this recently to create a custom module to save data to a new database table within drupal. To figure out how to set up the hook_schema, you can look at ".install" files within the drupal core and contributed codebase.
Then to create an edit or delete link, you need to alter your hook_menu to pass in the unique id for that element:
$items['admin/structure/nates-custom-page/update/%'] = array(
'title' => 'Update entry',
'page callback' => 'drupal_get_form',
'page arguments' => array('nates_module_form_update', 5),
'weight' => -5,
'access arguments' => array('administer DFP'),
);
$items['admin/structure/nates-custom-page/delete/%'] = array(
'title' => 'Delete entry',
'page callback' => 'drupal_get_form',
'page arguments' => array('nates_module_form_delete', 5),
'access arguments' => array('administer DFP'),
);
Above is an example where I added the argument (indicated by the percentage sign in the array key, ("%"), that will represent the unique id for that item I want to edit or delete.
Then alter the edit form to load the item you want to edit by the id.
And add a delete form.
Here's an example of a delete form:
function nates_module_form_delete($form, &$form_state) {
$entry = nates_module_entry_load_by_pid(arg(5));
$form = array();
$form['pid'] = array(
'#type' => 'value',
'#value' => arg(5),
);
$output = "";
foreach($entry as $key => $value) {
$output .= "<p><strong>$key</strong>: $value</p>";
}
$form['markup'] = array(
'#type' => 'markup',
'#markup' => $output,
);
return confirm_form(
$form,
t('Are you sure you want to delete this item? '),
'example/list',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
return $form;
}
Notice I return a confirmation form first.
And here's the submit handler function that processes it after they submit the request:
function nates_module_form_delete_submit($form, &$form_state) {
global $user;
nates_module_entry_delete($form_state['values']['pid']);
drupal_set_message(t("Deleted entry"));
}
Then, edit your listing page to add the edit links and delete links:
function nates_module_list() {
$output = '';
// Get all entries in the dfp_amobee2 table.
if ($entries = nates_module_entry_load_all()) {
$rows = array();
foreach ($entries as $entry) {
// Sanitize the data before handing it off to the theme layer.
$entry->editlink = l('edit','admin/structure/nates-custom-page/update/' . $entry->pid, array('query' => array('destination' => 'admin/structure/nates-custom-page')));
$entry->deletelink = l('delete','admin/structure/nates-custom-page/delete/' . $entry->pid, array('query' => array('destination' => 'admin/structure/nates-custom-page')));
unset($entry->pid);
if(!empty($entry->tid)) {
$entry->alias = l($entry->alias, 'taxonomy/term/'.$entry->tid);
}
else if(isset($entry->pages)) {
if(drupal_valid_path($entry->pages)) {
$entry->alias = l(drupal_lookup_path('alias', $entry->pages),$entry->pages);
} else {
$entry->alias = check_markup($entry->pages);
}
} else {
$entry->alias = "";
}
unset($entry->pages);
if(!$entry->tid) {
$entry->tid = "";
}
$rows[] = (array) $entry;
}
// Make a table for them.
$header = array(t('path'), t('tid'), t('dfp tag machine_name'),t('amobee_as'), '', '');
$output .= '<p>' . t('If the "tid" is filled in, amobee_as will be used on taxonomy pages and on node pages (where the main term id matches). If the path is filled it, will be used on that path. Path supercedes "tid" when there is a conflict.') . '</p>';
$output .= theme('table', array('header' => $header, 'rows' => $rows));
}
else {
drupal_set_message(t('No entries have been added yet.'));
}
return $output;
}
I'm having a hard time figure out how to paginate in cakePHP when i have activate admin routing.
I have localhost/myapp that is the main page
and localhost/myapp/admin that is tthe admin area. So I want to paginate on the fron end, so in the localhost/myapp index page.
So as the main index page is index of Typology Model.
in my app/Config/routes.php i have this routing for homepage:
Router::connect(
'/',
array(
'admin'=>false,
'controller' => 'typologies',
'action' => 'index', 'index'
)
);
PS: i Also Use slug for finding posts.
In my app/Controller/TypologyController I have this:
$this->Paginator->settings = array(
'joins' => array(
array(
'table' => 'items',
'alias' => 'Item',
'type' => 'LEFT',
'conditions' => array('Item.id = Typology.item_id')
)
),
'conditions' => array(
'Item.published' => 1,
'Typology.published' => 1
),
'limit' => 1,
'order' => array('Typology.sort' => 'ASC'),
'fields' => array('Item.*', 'Typology.*'),
'recursive' => 2
);
$typologies = $this->paginate('Typology');
$this->set(compact('typologies'));
I have set limit=>1 just for testing purposes.
In my View/Typologies/index.ctp i have this:
<?php
$result = '';
foreach ($typologies as $typology):
$id = h($typology['Typology']['id']);
$item_id = h($typology['Typology']['item_id']);
$title = h($typology['Typology']['title']);
$description = h($typology['Typology']['description']);
$thumbnail = h($typology['Typology']['thumbnail']);
$price = h($typology['Typology']['price']);
$typology_category_id = h($typology['Typology']['typology_category_id']);
$typology_condition_id = h($typology['Typology']['typology_condition_id']);
if (strlen($description)>330) {
$short_description = substr($description, 0, 327);
$description = $short_description."...";
}
$dir = "img/uploads/typology/thumbnails/";
if (file_exists( $dir . $thumbnail)) {
$typologyThumbnails = $dir . $thumbnail;
}
else {
$typologyThumbnails = "img/uploads/noimg.jpg";
}
if($thumbnail=='NULL' || $thumbnail=='') {
$typologyThumbnails = "img/uploads/noimg.jpg";
}
$result .= "<div class=\"item_shadow\">";
if ($logged_in) {
if ($typology['Typology']['published']==0 || $typology['Typology']['published']==false || $typology['TypologyItem']['published']==0 || $typology['TypologyItem']['published']==false) {
$result .= "<div class=\"badge red\"> non publicato </div>";
}
}
$result .= "<div class=\"item\" style=\"background-image:url({$typologyThumbnails});\">";
$result .= "<div class=\"item-content\">";
$result .= "<div class=\"item-top-content\">";
$result .= "<div class=\"item-top-content-inner\">";
$result .= "<div class=\"item-top-title\">";
$result .= "<h4>{$title}</h4>";
$result .= "</div>";
$result .= "</div>" ;
$result .= "</div>";
$result .= "<div class=\"item-add-content\">";
$result .= "<div class=\"item-add-content-inner\">";
$result .= "<div class=\"description-inner\">";
$result .= "<p>{$description}</p>";
$result .= "</div>";
$result .= "<div class=\"read-more-inner\">";
$result .= $this->Html->link("maggiori informazioni". $this->Html->image('elenco.png'), array('admin'=>false,'controller' => 'items', 'action' => 'view', 'slug' => Inflector::slug($typology['TypologyItem']['seo_url'],'-'),'id'=>$typology['Typology']['item_id'],'?'=> array('active_tab' => $typology['Typology']['id'])), array('escape' => false));
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
endforeach;
$result .= "";
?>
So as you see first i store all the data into a variable then i echo it like this with the paginator helper:
<?php
if (isset($result)) {
echo $result;
}
?>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page{:page} di {:pages}, Showing {:current} records of {:count} in total, starting at {:start}, and finishing at {:end}')
));
?>
</p>
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('Preview'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('Next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
So the first time i load the page, URL is like this http://localhost/mycakephpApp/ it displays one date like it suppose to:
When i click next, or number 2, URL is like this: http://localhost/mycakephpApp/typologies/index/index/page:2, it should display the next one. It does but now it is blank like this.
I don't know what am i missing or what am i doing wrong. Does anyone has any idea how to fix this?
I would really appriciate your your help.
Relative urls are problematic
The view contains the following:
$result .= "<div class=\"item\" style=\"background-image:url({$typologyThumbnails});\">";
Where $typologyThumbnails is a relative url of the form:
img/uploads/typology/thumbnails/whatever.jpg
That means that for the url http://localhost/mycakephpApp/ the following image url is requested:
http://localhost/mycakephpApp/img/uploads/typology/thumbnails/whatever.jpg
However, for the url http://localhost/mycakephpApp/typologies/index/index/page:2 the following image url is requested:
http://localhost/mycakephpApp/typologies/index/index/img/uploads/typology/thumbnails/whatever.jpg
Which doesn't exist.
There's a very simple solution:
Always use absolute urls i.e. /img/uploads/typology/thumbnails/whatever.jpg
Use helper functions and the router class, or at least recognise what they do.
You can fix the problem as asked by doing the following before using the variable:
$typologyThumbnails = Router::url('/' . $typologyThumbnails);
This will output:
/mycakephpApp/img/uploads/typology/thumbnails/whatever.jpg
Use helpers and elements
View files of the form:
echo "large chunk of html"
are rather hard to read/maintain. Instead the view code should be for example:
<?php
foreach ($typologies as $typology) {
echo $this->element('typology', array('row' => $typology));
}
// pagination links
And the body of the foreach loop in an element. But don't write large swathes of html as strings - it's a lot easier to read/maintain if it's written as:
<?php
// /App/View/elements/typology.ctp
// variable setup
?>
<div class="item_shadow">
<div class="item" style="background-image:url(<?= Router::url($typologyThumbnails) ?>);">
...
<h4><?= h($row['Typology']['title']) ?></h4>
It also has the added benefit that your editor will highlight the html.
I'm trying to register a theme function for a simple form in a custom module, but the theme function is not being called. I just get the basic form.
Here's my hook_theme():
function kss_membership_theme($existing, $type, $theme, $path){
$items = array();
$items['kss_membership_payment_form'] = array(
'render element' => 'form',
);
return $items;
}
for this form:
/**
* Returns the form for the second page of the payment process
*/
function kss_membership_payment_form($form, &$form_state) {
$form['description'] = array(
'#type' => 'item',
'#title' => t('We currently accept Paypal payments'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('kss_membership_payment_form_submit'),
);
$form['#theme'] = array('theme_kss_membership_payment_form');
return $form;
}
and here is the theme function:
function theme_kss_membership_payment_form($variables) {
// Isolate the form definition form the $variables array
$form = $variables['form'];
$output = '<h2>' . t('Please enter your information below') . '</h2>';
$output .= '<div id="personal_details">';
$output .= drupal_render($form['description']);
$output .= drupal_render($form['submit']);
// Additional template output goes here....
$output .= '</div>';
$output .= drupal_render_children($form);
return $output;
}
You are very near to solution, Only one problem is there.
theme calling is wrong
$form['#theme'] = array('theme_kss_membership_payment_form');
you are required to call
$form['#theme'] = array('kss_membership_payment_form');
After that You must required to clear the cache from the admin => configuration => Performance => Clear Cache button.
/**
* Returns the form for the second page of the payment process
*/
function kss_membership_payment_form($form, &$form_state) {
$form['description'] = array(
'#type' => 'item',
'#title' => t('We currently accept Paypal payments'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('kss_membership_payment_form_submit'),
);
$form['#theme'] = array('kss_membership_payment_form');
return $form;
}
What I am trying to do is display a table with checkboxes on the press of a button by ajax. The table should be initially hidden and get populated on the fly by a function call.
If initially I load $options1 with some dummy values , then after ajax call it throws in an error saying-
Notice: Undefined index: red in theme_tableselect() (line 3285 of
D:\wamp\www\drupal7\includes\form.inc).
where 'red' is the index of a dummy row value and #options don't get populated with the new values. What is the way to get this working ?
Here is the code for the form-
$form['mltag_new']['tag'] = array(
'#type' => 'button',
'#value' => t("Suggest Tags"),
'#ajax' => array(
'callback' => 'mltag_suggest_tags_ajax',
'wrapper' => 'mltag_suggest_tags_table_div',
'effect' => 'slide',
),
);
$options1 = array(); //initial dummy values
$options1['red']['tag'] = "A red row";
$options1['red']['chi'] = "A red row";
$form['mltag_new']['myselector'] = array (
'#type' => 'tableselect',
'#title' => 'My Selector',
'#header' => $header,
'#options' => $options1,
'#prefix' => '<div id="mltag_suggest_tags_table_div">',
'#suffix' => '</div>',
);
return $form;
and the Ajax callback looks something like this-
function mltag_suggest_tags_ajax($form, $form_state) {
//$content has some content
//pass the content to a function
include_once 'includes/content_tag.inc';
$tags = mltag_content_tag($content, variable_get('algo_type'), 20);
if (empty($tags)) {
$output .= t('Content is insufficient to generate Tags using this algorithm. <br>Please choose other algorithm from Settings Page.');
$form['mltag_new']['sample_text']['#markup'] = $output;
return $form['mltag_new']['sample_text'];
}
else {
$algo = variable_get('algo_type');
if ($algo == 1) {
$header = array(
'tag' => t('Tag'),
'frequency' => t('Frequency'),
);
$options = array();
foreach ($tags as $key => $value) {
$options[$key] = array(
'tag' => $key,
'frequency' => $value,
);
}
}
elseif ($algo == 2) {
$header = array(
'tag' => t('Tag'),
'chi' => t('Chi Square Value'),
);
$options = array();
foreach ($tags as $key => $value) {
$options[$key] = array(
'tag' => $key,
'chi' => $value,
);
}
}
$form['mltag_new']['myselector']['#header'] = $header;
$form['mltag_new']['myselector']['#options'] = $options;
return $form['mltag_new']['myselector'];
}
}
I replied to your post on Drupal.org about how I'm working on a somewhat similar problem. Try adding
$form['mltag_new']['myselector'] =
form_process_tableselect($form['mltag_new']['myselector']);
just before your return. Hopefully that helps you more than it did me. Beware that the #options just get rendered when the block reloads from the ajax, but the original $form object doesn't seem to be aware.
I know that this is a few years later, but I found this while searching for my own solution:
The tableselect module creates checkboxes in the $ form that have to be removed. in the example above, they would be in $form['mltag_new']['myselector'] with keys equal to the original $option1 in your original code. If you unset those, then call
$form['mltag_new']['myselector'] = form_process_tableselect($form['mltag_new']['myselector']);
before your return, it will eliminate the dummy rows.
I am creating a custom module that I would like to use to display a form, from a module. The form queries a database and displays some information. The database connection is working, and the data has been retrieved successfully. The form has also been displayed successfully, but what I can figure out is how to link all together to have the page displayed again when submitting the form. My code is:
<?php
function menu($may_cache) {
$items = array();
$items['admin/reporting/report_details'] = array(
'title' => 'Report: User details by stores',
'access arguments' => array('access content'),
'page callback' => 'say_report_details',
'type' => MENU_CALLBACK,
);
return $items;
}
// This function should execute the logic if the $_GET variable is set
function say_report_details($values = array()) {
// The $_GET logic will be somtehing like this
// if (count($_GET) > 0)
// Get all the form values from the $_GET wit something like:
// if (count($_GET) > 0) {
// $start = mktime(0, 0, 0, $_GET['from_month'], $_GET['from_day'],
$_GET['from_year']);
// $end = mktime(23, 59, 59, $_GET['to_month'], $_GET['to_day'], $_GET['to_year']);
if ($_GET['store'] > 0) {
$form = drupal_get_form("report_details_form");
$output = theme("report_page", $form, $output);
return $output;
}
function report_details_form() {
$form["search"] = array(
'#type' => 'fieldset',
'#title' => t('Search params'),
'#collapsible' => FALSE,
'#tree' => TRUE,
);
for ($i = 1; $i < 32; $i++) {
$days_opt[$i] = $i;
}
for ($i = 1; $i < 13; $i++) {
$month_opt[$i] = $i;
}
for ($i = 2008; $i < date("Y") + 3; $i++) {
$year_opt[$i] = $i;
}
$form["search"]["from_day"] = array(
'#type' => 'select',
'#title' => 'Date from',
'#options' => $days_opt,
'#default_value' => (($_GET['from_day'] == "") ? date("d") : $_GET['from_day'])
);
$form["search"]["from_month"] = array(
'#type' => 'select',
'#title' => ' ',
'#options' => $month_opt,
'#default_value' => (($_GET['from_month'] == "") ? date("m") : $_GET['from_month'])
);
$form["search"]["from_year"] = array(
'#type' => 'select',
'#title' => ' ',
'#options' => $year_opt,
'#default_value' => (($_GET['from_year'] == "") ? date("Y") : $_GET['from_year'])
);
$form["search"]["to_day"] = array(
'#type' => 'select',
'#title' => 'Date to',
'#options' => $days_opt,
'#default_value' => (($_GET['to_day'] == "") ? date("d") : $_GET['to_day'])
);
$form["search"]["to_month"] = array(
'#type' => 'select',
'#title' => ' ',
'#options' => $month_opt,
'#default_value' => (($_GET['to_month'] == "") ? date("m") : $_GET['to_month'])
);
$form["search"]["to_year"] = array(
'#type' => 'select',
'#title' => ' ',
'#options' => $year_opt,
'#default_value' => (($_GET['to_year'] == "") ? date("Y") : $_GET['to_year'])
);
$result = db_query('SELECT taxonomy_term_data.name, taxonomy_term_data.tid FROM
taxonomy_term_data WHERE vid = 10');
$strs = array("all" => "All");
foreach ($result as $store) {
$strs[$store->tid] = $store->name;
}
$form["search"]["store"] = array(
'#type' => 'select',
'#title' => 'Stores',
'#options' => $strs,
'#default_value' => $_GET['store']
);
$form["submit"] = array("#type" => "submit", "#value" => "Show report");
return $form;
}
function theme_report_page($form, $result = array()) {
$output = '
<div id="report_form">
'. $form .'
</div>
<div id="report_result">
'. $result .'
</div>
';
return $output;
}
function theme_report_details_form($form) {
unset($form['search']['from_day']['#title']);
unset($form['search']['from_month']['#title']);
unset($form['search']['from_year']['#title']);
unset($form['search']['to_day']['#title']);
unset($form['search']['to_month']['#title']);
unset($form['search']['to_year']['#title']);
unset($form['search']['store']['#title']);
$output = "<fieldset>
<legend>Search params</legend>
<div class='fieldtitles'>". t('Date From') .":</div>
". drupal_render($form['search']['from_day']) ."
". drupal_render($form['search']['from_month']) ."
". drupal_render($form['search']['from_year']) ."
<div class='clearing'> </div>
<div class='fieldtitles'>". t('Date To') .":</div>
". drupal_render($form['search']['to_day']) ."
". drupal_render($form['search']['to_month']) ."
". drupal_render($form['search']['to_year']) ."
<div class='clearing'> </div>
<div class='fieldtitles'>". t('Store') .":</div>
". drupal_render($form['search']['store']) ."
<div class='clearing'> </div>
". drupal_render($form['submit']) ."
</fieldset>";
unset($form['search']);
$output .= drupal_render($form);
return $output;
}
function report_details_form_submit($form_id, $form_values) {
$query = "";
// This next line is the one we are having an issue right now, as is trowing an
error due to $form_values is empty for some UNKNOWN reason...
foreach ($form_values['search'] as $key => $value) {
$query .= "&". $key ."=". $value;
}
return array('admin/reporting/report_details', $query);
}
I did not look over all the details of the form, but the submit function for D7 should look like this:
function my_module_example_form_submit($form, &$form_state) {
// do something
}
I think that should fix the problem. If not check out this link: http://api.drupal.org/api/drupal/includes%21form.inc/group/form_api/7
Please read & follow the answer mikewink provided.
In addition in the three working forms that I've created the menu item array is as follows:
$items = array();
$items['admin/config/system/dataforms/SiteAccessform']=array(
'title'=>'Site Access' //this should be watever the Title of your form is
,'description'=>'Configuration for the Data Forms module' //shows up by your menu item as it's description (where applicable)
,'page callback'=>'drupal_get_form' //this, as I understand it, is required for forms to work properly (& may be part of the cause of your problems)
,'page arguments'=>array('dataforms_main_form') //IMPORTANT this is the actual name of your function that will be executed to produce the form
,'access arguments'=>array('administer settings for dataforms') //??
,'type'=>MENU_CALLBACK//these work when the path is accessed though no menu items are created
,
);
A few more points, when you are on a particular 'menu' item with it's page arguments array element set to your form, it considers it ONE form only. That is to say that the one function will be the only function that will be able to create a form (in my experience). Therefore use logic in that function to produce different forms if applicable... ,(as opposed to attempting to have a button that calls another function... which did not work for me).
Also, again referring back to mikewink's answer, the $form_state variable is the method of storing state that you want to pay attention, & not the standard $_GET or $_POST variables.
This link might be mildly helpful: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7
However I gleaned the most valuable information from the examples module: https://drupal.org/project/examples
Good Luck!