I've been trying to figure this out and make it work, but as I'm not a programmer, just a designer with limited coding knowledge I've been hitting a wall on this one.
I'm making my own photo gallery and I would like to display two previous or next images as linked thumbnails. I've found this script that works and shows textual links, but don't know how to adapt it to my needs.
template.php
<?php
function dad_prev_next($current_node = NULL, $op = 'p') {
if ($op == 'p') {
$sql_op = '<';
$order = 'DESC';
} elseif ($op == 'n') {
$sql_op = '>';
$order = 'ASC';
} else {
return NULL;
}
$output = NULL;
$sql = "SELECT n.nid, n.title
FROM {node} n
WHERE n.nid $sql_op :nid
AND type IN ('photos')
AND status = 1
ORDER BY nid $order
LIMIT 1";
$result = db_query($sql, array(':nid' => $current_node -> nid));
foreach ($result as $data) {
}
if (isset($data)) {
if ($op == 'n')
return l("Next", "node/$data->nid", array('html' => TRUE));
else if ($op == 'p')
return l("Previous", "node/$data->nid", array('html' => TRUE));
}
}
?>
node.tpl.php
<?php print dad_prev_next($node,'p') . " / " . dad_prev_next($node,'n'); ?>
Working example
This one shows two previous/next images as it was designed
template.php
function dad_prev_next($nid = NULL, $op = 'p', $start = 0) {
if ($op == 'p') {
$sql_op = '>';
$order = 'ASC';
}
elseif ($op == 'n') {
$sql_op = '<';
$order = 'DESC';
}
else {
return NULL;
}
$output = '';
// your node must have an image type field
// let's say it's name is IMAGEFIELD
// select from node table
$query = db_select('node', 'n');
// join node table with image field table
$query->leftJoin('field_data_field_photo', 'i', 'i.entity_id = n.nid');
// join file managed table where all data about managed files stored
$query->leftJoin('file_managed', 'f', 'f.fid = i.field_photo_fid');
$query
// select nid and title from node
->fields('n', array('nid', 'title'))
// select uri from file_managed (image path)
->fields('f', array('uri'))
// select image alt and title
->fields('i', array('field_photo_alt', 'field_photo_title'))
// where nid "greater than"/"lower than" our current node nid
->condition('n.nid', $nid, $sql_op)
// where node type in array('your content types')
->condition('n.type', array('photos'), 'IN')
// where node is published
->condition('n.status', 1)
// where requested node has image to display (if you want thumbnail)
->condition('f.uri', '', '!=')
// order by nid
->orderBy('n.nid', $order)
// limit result to 1
->range($start, 1);
// make query
$result = $query->execute()->fetchAll();
foreach ($result as $node) {
// theme your thumbnail image
$variables = array(
// default image style name `thumbnail`
// you can use your own by following
// admin/config/media/image-styles on your site
'style_name' => 'square_small',
'path' => $node->uri,
'alt' => $node->field_photo_alt,
'title' => $node->field_photo_title
);
$image = theme('image_style', $variables);
$options = array(
'html' => TRUE,
'attributes' => array(
'title' => $node->title
)
);
$output = l($image, "node/{$node->nid}", $options);
}
return $output;
}
node.tpl.php
<?php print dad_prev_next($node->nid, 'p', 1); ?>
<?php print dad_prev_next($node->nid, 'p', 0); ?>
<?php print dad_prev_next($node->nid, 'n', 0); ?>
<?php print dad_prev_next($node->nid, 'n', 1); ?>
template.php
function dad_prev_next($nid = NULL, $op = 'p', $qty = 1) {
if ($op == 'p') {
$sql_op = '<';
$order = 'DESC';
}
elseif ($op == 'n') {
$sql_op = '>';
$order = 'ASC';
}
else {
return NULL;
}
$output = '';
// your node must have an image type field
// let's say it's name is IMAGEFIELD
// select from node table
$query = db_select('node', 'n');
// join node table with image field table
$query->leftJoin('field_data_field_IMAGEFIELD', 'i', 'i.entity_id = n.nid');
// join file managed table where all data about managed files stored
$query->leftJoin('file_managed', 'f', 'f.fid = i.field_IMAGEFIELD_fid');
$query
// select nid and title from node
->fields('n', array('nid', 'title'))
// select uri from file_managed (image path)
->fields('f', array('uri'))
// select image alt and title
->fields('i', array('field_IMAGEFIELD_alt', 'field_IMAGEFIELD_title'))
// where nid "greater than"/"lower than" our current node nid
->condition('n.nid', $nid, $sql_op)
// where node type in array('your content types')
->condition('n.type', array('photos'), 'IN')
// where node is published
->condition('n.status', 1)
// where requested node has image to display (if you want thumbnail)
->condition('f.uri', '', '!=')
// order by nid
->orderBy('n.nid', $order)
// limit result to $qty
->range(0, $qty);
// make query
$result = $query->execute()->fetchAll();
foreach ($result as $node) {
// theme your thumbnail image
$variables = array(
// default image style name `thumbnail`
// you can use your own by following
// admin/config/media/image-styles on your site
'style_name' => 'thumbnail',
'path' => $node->uri,
'alt' => $node->field_IMAGEFIELD_alt,
'title' => $node->field_IMAGEFIELD_title
);
$image = theme('image_style', $variables);
$options = array(
'html' => TRUE,
'attributes' => array(
'title' => $node->title
)
);
$output = l($image, "node/{$node->nid}", $options);
}
return $output;
}
node.tpl.php
<?php print dad_prev_next($node->nid, 'p', 2); ?>
<?php print dad_prev_next($node->nid, 'n', 2); ?>
Related
I have an entityform block and I want to add bootstrap classes to its input!
How can I do this and what files should I edit?
I'm using latest version of entityform and entity block on drupal.org
thank you
You need to add these functions to your template.php file inside your theme folder (replace THEME_NAME with the actual name of your theme):
function THEME_NAME_textfield($variables) {
$element = $variables['element'];
$element['#attributes']['type'] = 'text';
element_set_attributes($element, array('id', 'name', 'value', 'size', 'maxlength'));
_form_set_class($element, array('form-text'));
$element['#attributes']['class'][] = 'form-control';
$extra = '';
if ($element['#autocomplete_path'] && drupal_valid_path($element['#autocomplete_path'])) {
drupal_add_library('system', 'drupal.autocomplete');
$element['#attributes']['class'][] = 'form-autocomplete';
$attributes = array();
$attributes['type'] = 'hidden';
$attributes['id'] = $element['#attributes']['id'] . '-autocomplete';
$attributes['value'] = url($element['#autocomplete_path'], array('absolute' => TRUE));
$attributes['disabled'] = 'disabled';
$attributes['class'][] = 'autocomplete';
$extra = '<input' . drupal_attributes($attributes) . ' />';
}
$output = "<div class='input-wrapper'>";
$output .= '<input' . drupal_attributes($element['#attributes']) . ' />';
$output .= "</div>";
return $output . $extra;
}
function THEME_NAME_button($variables) {
$element = $variables['element'];
$element['#attributes']['type'] = 'submit';
element_set_attributes($element, array('id', 'name', 'value'));
$element['#attributes']['class'][] = 'form-' . $element['#button_type'];
$element['#attributes']['class'][] = 'btn btn-default';
if (!empty($element['#attributes']['disabled'])) {
$element['#attributes']['class'][] = 'form-button-disabled';
}
$value = $element['#attributes']["value"];
if( isset($element['#attributes']["button-type"]) && $element['#attributes']["button-type"] == "search" ) {
$value = $element['#attributes']["icon"];
unset($element['#attributes']["icon"]);
}
return '<button' . drupal_attributes($element['#attributes']) . '>' . $value . '</button>';
}
function THEME_NAME_select($variables) {
$element = $variables['element'];
element_set_attributes($element, array('id', 'name', 'size'));
_form_set_class($element, array('form-select', 'form-control'));
return '<select' . drupal_attributes($element['#attributes']) . '>' . form_select_options($element) . '</select>';
}
These functions will add the classes you need for the button, textfield and select input types. Also you will need to include the bootstrap files in your theme.
I'm trying to create a module that will display some last entries from database. I'd like to send last entry object to template file (guestbook-last-entries.tpl.php), that looks like that
<p><?php render($title); ?></p>
<?php echo $message; ?>
I have a function that implements hook_theme
function guestbook_theme() {
return array(
'guestbook_last_entries' => array(
'variables' => array(
'entries' => NULL,
),
'template' => 'guestbook-last-entries'
),
);
}
one that do preprocess
function template_preprocess_guestbook_last_entries(&$variables) {
$variables = array_merge((array) $variables['entries'], $variables);
}
and functions that implements hook_block_view
function guestbook_block_view($delta = '') {
switch ($delta) {
case 'guestbook_last_entries':
$block['subject'] = t('Last entries');
$block['content'] = array();
$entries = guestbook_get_last_entries(variable_get('guestbook_m', 3));
foreach ($entries as $entry) {
$block['content'] += array(
'#theme' => 'guestbook_last_entries',
'#entries' => $entry,
);
}
break;
}
return $block;
}
function that gets data from database
function guestbook_get_last_entries($limit = 3) {
$result = db_select('guestbook', 'g')
->fields('g')
->orderBy('posted', 'DESC')
->range(0, $limit)
->execute();
return $result->fetchAllAssoc('gid');
}
But in this case I get only one entry displayed. Can anyone tell me how to resolve this, how should I build that $block['content']?
Thank you
This here wont work:
$block['content'] += array(
'#theme' => 'guestbook_last_entries',
'#entries' => $entry,
);
Maybe you want this if you need an array as the result:
// note that I replaced += with a simple = and added two brackets that will create a new element in that array $block['content']
$block['content'][] = array(
'#theme' => 'guestbook_last_entries',
'#entries' => $entry,
);
I am able to obtain the $data array from a form and I can generate data for each item... But during the binding and storing with jTable object it saves only the latest item why... I am using native JTableMenu class to bind and save by using my own class which is extended by native one. Early trials I used database object to save items with sql syntax but some columns of table remains empty lft and rgt to fill them I used table object but it gives this issue.
The whole code is following:
function addcumulative($data){
$db = JFactory::getDBO();
$component = & JComponentHelper::getComponent('com_dratransport');
$menus = array();
$query = array();
$countries = DraTransportHelperArrays::countries();
$cities = DraTransportHelperArrays::cities();
$title = array();
$alias = array();
$path = array();
$link = array();
if(empty($data['parent_id']) && $data['parent_id'] == 0){
$data['parent_id'] = 1;
}else{
$parent_id = explode('.',$data['parent_id']);
$data['parent_id'] = $parent_id[1];
}
if(!empty($data['locationQuery'])){ //actually this part will be used
$loc = ($data['locationQuery'] == 'countries') ? $countries : $cities['Turkey'] ;
foreach($loc as $k => $c){
$query[0] = $data['general'];
foreach($data as $key => $dat){
if(!empty($dat) && strpos($key,'Query') !== false){
$v = explode('Q',$key);
if($v[0] !== 'location'){
$query[] = '&'.$v[0].'='.$dat;
}else{
$query[] = '&'.$dat.'='.$k;
}
}
}
$title[] = $data['viewQuery'].'-'.$k;
$alias[] = $data['viewQuery'].'-'.$k;
$path[] = $data['viewQuery'].'-'.$k;
$link[] = implode('',$query);
$query = array();
}
}else{
$query[0] = $data['general'];
foreach($data as $key => $dat){
if(!empty($dat) && strpos($key,'Query') !== false){
$v = explode('Q',$key);
$query[] = '&'.$v[0].'='.$dat;
}
}
$link[] = implode('',$query);
}
foreach($link as $n => $l){
$menus[] = array(
'menutype' => $data['menutype'],
'title' => $title[$n],
'alias' => $alias[$n],
'path' => $path[$n],
'link' => $link[$n],
'type' => 'component',
'published' => 1,
'parent_id' => $data['parent_id'],
'level' => 1,
'component_id' => $component->id,
'access' => $data['access'],
'params' => $data['params'],
'language' => '*'
);
}
$count = $data['count'] == 0 ? count($loc) : $data['count'];
foreach($menus as $menu){
// Bind the data.
$table = $this->getTable();
$table->bind($menu);
$table->store();
}
}
Yes you are totally right. Let say $countries=array('England','France','Germany'); then I wrote my code in model to generate links in #__menu table. So I write
foreach($countries as $country){
$link='index.php&option=com_mycomponent&view=members&type=1&countries='.$country;
$table->bind();
$table->save();
}
I am generating links like this, view and the type values in query comes from the form submitted to make parmaters same for all menus except countries;
and I assign all parametes to manu item and menu items to an array as array item...
to save database the table comes with nested.
$menus->$each menu as $menus array item->menu item parameters
while the table is first created in above code code just adds the last menu item but if I take it insede the foreach it adds all of them but parent_id and level parameters assigned to 0 bye the native code altought I set them as 1
app\views\images/view.ctp
this is the views code ..i need help to fetch image
<?php
// initialise a counter for striping the table
$count = 0;
// loop through and display format
foreach($images as $image){
// stripes the table by adding a class to every other row
$class = ( ($count % 2) ? " class='altrow'": '' );
// increment count
$count++;
$full3 = '/img/images/'.$image['Image'];
$thumb3 = $this->Html->image('images/image-1.jpg',array("width"=>"60", "height"=>"40"));
var_dump($full3) ;
$this->Html->link($thumb3,$full3,array('escape' => false, 'rel' => 'lightbox[plants]','title'=>'thanks allah for help me'));
}
?>
the controller code is images_controller code..
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid image', true));
$this->redirect(array('action' => 'index'));
}
$this->set('image', $this->Image->read(null, $id));
$images= $this->Image->find('all');
$this->set('images', $images);
}
replace this line
echo $thumb3 = $this->Html->image('images"/".$image['Image']['img_file']',array('width'=>"60", 'height'=>"40"));
with
echo $thumb3 = $this->Html->image("images/".$image['Image']['img_file'],array("width"=>"60", "height"=>"40"));
I have these codes on my add view
$status = array('0' => 'Resolved', '1' => 'Assigned/Unresolved', '2' => 'Suspended', '3' => 'Closed', '4' => 'Bypassed');
echo $this->Form->input('status', array('options' => $status));
and instead of saving the value (e.g. Resolved) to the table, it saves the index of the array. Any ideas?
You should do something like this:
$status = array('resolved' => 'Resolved', 'assigned' => 'Assigned/Unresolved'...);
It saves the index of the array. But this is not a good practice, instead try using enums. Check this out:
/**
* Get Enum Values
* Snippet v0.1.3
* http://cakeforge.org/snippet/detail.php?type=snippet&id=112
*
* Gets the enum values for MySQL 4 and 5 to use in selectTag()
*/
function getEnumValues($columnName=null, $respectDefault=false)
{
if ($columnName==null) { return array(); } //no field specified
//Get the name of the table
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$tableName = $db->fullTableName($this, false);
//Get the values for the specified column (database and version specific, needs testing)
$result = $this->query("SHOW COLUMNS FROM {$tableName} LIKE '{$columnName}'");
//figure out where in the result our Types are (this varies between mysql versions)
$types = null;
if ( isset( $result[0]['COLUMNS']['Type'] ) ) { $types = $result[0]['COLUMNS']['Type']; $default = $result[0]['COLUMNS']['Default']; } //MySQL 5
elseif ( isset( $result[0][0]['Type'] ) ) { $types = $result[0][0]['Type']; $default = $result[0][0]['Default']; } //MySQL 4
else { return array(); } //types return not accounted for
//Get the values
$values = explode("','", preg_replace("/(enum)\('(.+?)'\)/","\\2", $types) );
if($respectDefault){
$assoc_values = array("$default"=>Inflector::humanize($default));
foreach ( $values as $value ) {
if($value==$default){ continue; }
$assoc_values[$value] = Inflector::humanize($value);
}
}
else{
$assoc_values = array();
foreach ( $values as $value ) {
$assoc_values[$value] = Inflector::humanize($value);
}
}
return $assoc_values;
} //end getEnumValues
Paste that method in your AppModel.
Create the column in your table as an enum with the posible values, and use that method to get them.
Can you not define the index as the value you want?
$status = array('Resolved' => 'Resolved', 'Assigned/Unresolved' => 'Assigned/Unresolved', etc etc );
echo $this->Form->input('status', array('options' => $status));