Skip to content

Instantly share code, notes, and snippets.

@mavimo
Created September 13, 2012 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mavimo/3713600 to your computer and use it in GitHub Desktop.
Save mavimo/3713600 to your computer and use it in GitHub Desktop.
<strong>TEST</strong>
<pre>
<?php print_r($dati); ?>
</pre>
(function ($){
Drupal.behaviors.test_js = {
attach: function(context, settings) {
$('#edit-test-prova').change(function() {
console.log('cambiato');
})
}
}
})(jQuery);
name = Test HP
description = Creo un modulo di test
core = 7.x
<?php
/**
* Implements hook_schema().
*/
function test_hp_schema() {
$schema['test_hp'] = array(
'description' => 'Tabella per l\'inserimento di informazioni di prova',
'fields' => array(
'hpid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Unique ID.',
),
'informazione' => array(
'description' => 'Testo inserito dall\'utente',
'type' => 'varchar',
'length' => 255,
),
'scelta' => array(
'description' => 'Il valore scelto',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'indexes' => array(
'scelta' => array('scelta'),
),
'primary key' => array('hpid'),
);
return $schema;
}
<?php
/**
* Implements hook_menu().
*/
function test_hp_menu() {
$items = array();
$items['test/hp'] = array(
'title' => 'Test HP',
'page callback' => 'test_hp_pagina',
'access arguments' => array('access content'),
);
$items['test/hp/%'] = array(
'title' => 'Test HP',
'page callback' => 'test_hp_pagina_parametro',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
// 'file' => ,
);
$items['test/hp2'] = array(
'title' => 'Test HP',
'page callback' => 'test_hp_pagina2',
'page arguments' => array(),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
// 'file' => ,
);
$items['test/hp_node/%node'] = array(
'title' => 'Test HP',
'page callback' => 'test_hp_pagina_node',
'page arguments' => array(2),
// 'access callback' => 'test_hp_pagina_access',
'access arguments' => array('test_hp show node'),
'type' => MENU_NORMAL_ITEM,
// 'file' => ,
);
$items['test/form'] = array(
'title' => 'Form TEST',
// 'page callback' => 'test_hp_altro_form',
'page callback' => 'drupal_get_form',
'page arguments' => array('test_hp_form'),
'access arguments' => array('test_hp show node'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_permission().
*/
function test_hp_permission() {
return array(
'test_hp show node' => array(
'title' => t('Visualizza print_r nodi'),
'description' => t('Permette di visualizzare la struttura dei nodi'),
),
);
}
function test_hp_pagina() {
return "prova";
}
function test_hp_pagina2() {
print "prova2";
exit();
}
function test_hp_pagina_node($node) {
return '<pre>' . print_r($node, TRUE) . '</pre>';
}
function test_hp_pagina_parametro($parametro) {
return "Parametro $parametro";
}
function test_hp_pagina_access() {
return FALSE;
}
/**
* Implements hook_block_info().
*/
function test_hp_block_info() {
$blocks['test_hp_1'] = array(
'info' => t('Blocco di prova 1'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function test_hp_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'test_hp_1':
$dati = test_hp_blocco();
$block['subject'] = t('Prova HP');
$block['content'] = theme(
'item_list', array(
'items' => $dati
)
);
break;
}
return $block;
}
/**
* Implements hook_theme().
*/
function test_hp_theme($existing, $type, $theme, $path) {
return array(
'mio_tema' => array(
'variables' => array('dati' => NULL),
'template' => 'mio-tema',
),
);
}
function test_hp_blocco() {
return array('primo', 'secondo');
}
// function theme_mio_tema($variables) {
// return '<pre>' . print_r($variables, TRUE) . '</pre>';
// }
function test_hp_process_item_list(&$variables) {
foreach ($variables['items'] as &$item) {
$item .= ' X';
}
$variables['attributes']['class'] = array('prova-classe');
}
function test_hp_process_node(&$variables) {
$variables['title'] .= " aaaa";
}
function test_hp_form() {
$form = array();
$id = arg(2);
// $res = db_query("SELECT * FROM {test_hp} WHERE hpid = %d", $id);
// // while($data = db_fetch_object($res)) {
// while($data = db_fetch_array($res)) {
// dpm($data);
// }
$res = db_select('test_hp', 'hp')
->fields('hp', array(
'hpid',
'informazione',
'scelta',
))
->condition('hpid', $id, '=')
->execute();
$data = $res->fetchAll();
$data = array_pop($data);
$form['#data_test_hp'] = $data;
$form['test_insert'] = array(
'#type' => 'textfield',
'#title' => 'Inserisci',
'#default_value' => isset($data->informazione) ? $data->informazione : '',
);
$form['test_select'] = array(
'#type' => 'select',
'#title' => 'Scegli',
'#empty_option' => 'Scegli il valore',
'#empty_value' => '-1',
'#default_value' => isset($data->scelta) ? $data->scelta : '',
'#options' => array(
0 => 'aaaa',
1 => 'bbbb',
2 => 'cccc',
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'test_hp') . '/js/test.js',
),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Invia'),
);
return $form;
}
function test_hp_form_validate(&$form_state, $form_values) {
if ($form_values['values']['test_select'] == 2) {
form_set_error('test_select', t('Il valore non va bene'));
}
}
function test_hp_form_submit($form, $form_state) {
$insert = $form_state['values']['test_insert'];
$select = $form_state['values']['test_select'];
// db_query("INSERT INTO {test_hp} (informazione, scelta) VALUES ('%s', %d)", $insert, $select);
$data = $form['#data_test_hp'];
if (is_null($data)) {
$query = db_insert('test_hp')
->fields(array(
'informazione' => $insert,
'scelta' => $select,
))
->execute();
}
else {
$query = db_update('test_hp')
->fields(array(
'informazione' => $insert,
'scelta' => $select,
))
->condition('hpid', $data->hpid, '=')
->execute();
}
// DRUPAL 6
// // INSERT
// // 1
// db_query("INSERT INTO {test_hp} (informazione, scelta) VALUES ('%s', %d)", $insert, $select);
// // 2
// $data = new stdClass();
// $data->informazione = $insert;
// $data->scelta = $select;
// drupal_write_record('test_hp', $data);
// // 3
// $data = array(
// 'informazione' => $insert,
// 'scelta' => $select,
// );
// drupal_write_record('test_hp', $data);
// // UPDATE
// drupal_write_record('test_hp', $data, array('hpid'));
}
function test_hp_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'search_block_form') {
// dpm($form);
}
}
function test_hp_form_search_block_form_alter(&$form, &$form_state, $form_id) {
$form['search_block_form']['#description'] = t('Inserisci le parole da cercare');
}
// function test_hp_preprocess_block(&$variables) {
// $form = drupal_get_form('test_hp_form');
// $variables['content'] = drupal_render($form);
// }
// db_query("DELETE ... WHERE hpid = %d", $id);
// db_delete('test_hp')->condition('hpid', $id, '=')->execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment