Pass variables from one page to a webform in drupal - php

I'm trying to pass one variable from different product pages to a webform page. Each product page has a session variable like $_SESSION["product_name"] = "Product1";. Similarly, other product pages have $_SESSION["product_name"] = "Product2"; $_SESSION["product_name"] = "Product3";
My question is how to pass these variables to a different webform page. I'm using "webform" module, what I want to accomplish is there should be a <select> tag, and there are options like
<select>
<option value="<?php echo $_SESSION["product_name"]; ?>" selected><? php echo $_SESSION["product_name"]; ?></option>
<option value="Product2">Product2</option>
<option value="Product3">Product3</option>
<option value="Product4">Product4</option>
</select>
The problem is there is no place for me to place these codes, is there any way around this or is there any module I can use for this issue?

You can create a custom module and then implement hook_form__alter()
You would need to find the IDs of the webforms you want to target.
Then:
function mymodule_form_webform1_alter(&$form, &$form_state) {
array_unshift($form['products']['#options'], array($_SESSION["product_name"] => $_SESSION["product_name"]));
}
or similar

This might be a way to pass the session value to webform option default value. I'm using Drupal 7, Webform 4. The session values on different product pages are:
$_SESSION["j_product_name"] = "haha1";
$_SESSION["j_product_name"] = "haha2";
First I created a custom module, the .module part is(also need to create .info):
<?php
function jay_tokens_token_info() {
$types['jay_token'] = array(
'name' => t("Jay Tokens"),
'description' => t("Jay custom tokens"),
);
$jay_token['product_session'] = array(
'name' => t("Product Name"),
'description' => t("product_name session value"),
);
return array(
'types' => $types,
'tokens' => array(
'jay_token' => $jay_token,
),
);
}
/**
* Implements hook_tokens().
*/
function jay_tokens_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'jay_token') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'product_session':
$replacements[$original] = $_SESSION["j_product_name"];
break;
}
}
}
return $replacements;
}
?>
In webform select options, the default value should be [jay_token:product_session] and options should be:
haha1|Product1
haha2|Product2

Related

Drupal custom modules and cache issue

We created a few custom custom modules for a Drupal site and we have the following issue : each time we create or update a content (whatever it is), the content generated by the custom modules disappears. We have to clear all caches to get this content to appear again.
As it is our first experience with Drupal, we are certainly missing something but we don't know what.
Any help would be greatly appreciated!
Below is the code of one of these custom modules:
File website_actualites.module
<?php
/**
* Implements hook_block_info().
*/
function website_actualites_block_info() {
$blocks['website_actualites'] = array(
'info' => t('website_actualites'),
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function website_actualites_block_view($delta = '') {
$aDelta = explode('+', $delta);
$nbActualite = 2;
if (!empty($aDelta[1])) {
$nbActualite = $aDelta[1];
}
$block = null;
switch ($aDelta[0]) {
case 'website_actualites':
$block['content'] = _website_actualites_sweet_block_content($nbActualite);
break;
}
return $block;
}
/**
* Callback implemented by hook_block_view().
*/
function _website_actualites_sweet_block_content($nbActualite=2) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->propertyCondition('status', 1)
->fieldCondition('field_mise_en_avant', 'value', 1)
->propertyOrderBy('created', 'DESC')
->range(0, $nbActualite)
->addMetaData('account', user_load(1));
$result = $query->execute();
// width col pour nb actu=2 : 4,8
// width col pour nb actu=4: 2,4,2,4
$colWidthEven=4;
$colWidthOdd=8;
if (4 == $nbActualite) {
$colWidthEven=2;
$colWidthOdd=4;
}
$data = array();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
$items = entity_load('node', $nids);
$i=0;
foreach ($items as $item) {
$colWidth=$colWidthOdd;
if (0 == $i%2) {
$colWidth = $colWidthEven;
}
$i++;
$data[$item->nid] = array(
'title' => $item->title,
'tags' => isset($item->fielsTags['und'][0]) ? $item->fielsTags['und'][0] : '',
'body' => isset($item->body['und'][0]['value']) ? $item->body['und'][0]['value'] : '',
'image' => isset($item->field_image['und'][0]) ? $item->field_image['und'][0] : '',
'nid' => $item->nid,
'col-width' => $colWidth,
'alias' => drupal_get_path_alias('node/'.$item->nid)
);
}
}
$static_title = t('Static Title');
$static_content = 'static content';
return theme('website_actualites_output', array(
'title' => $static_title,
'content' => $static_content,
'data' => $data
)
);
}
File website_actualite-sweet--block.tpl.php
<?php
foreach ($data as &$row) {
$url = drupal_get_path_alias('node/' . $row['nid']);
$imageWrapper = file_stream_wrapper_get_instance_by_uri($row['image']['uri']);
if (is_object($imageWrapper) && is_callable(array($imageWrapper, 'getExternalUrl'))) {
$imageUrl = $imageWrapper->getExternalUrl();
print '<div class="col-sm-'.$row['col-width'].'">
<div class="img">
<img src="' . $imageUrl . '" class="img-responsive" alt="image description">
</div>
<p>' . $row['title'] . '</p>
</div>';
}
}
first of all there are several problems with this code. If you're going to use Drupal then you need to stick to Drupal coding standards. Avoid using camel casing for variables, instead you should use underscores. Also you should use 2 spaces for indentation.
In your block info function you define a single block however you assign it to an unassigned variable:
// Define $blocks first.
$blocks = array();
$blocks['website_actualites'] = array(
Next issue is that you call hook_block_view($delta), this call will be fired for every block delta, and regardless of the block delta you are setting it to null (also for future reference should be NULL):
$block = null;
So Drupal is building the render array for each block, and you are wiping all the data. You are doing this because you are trying to use the block delta as a way to pass a parameter into your block with a delta like website_actualites+10. Blocks aren't designed to work this way and delta's are meant to be static so that Drupal can keep track of them in the database and perform the correct caching according to your cache flag. You've defined the delta website_actualites in your hook_block_info() but Drupal has no knowledge or configuration information for any other block delta.
If you have a need for the same block to display varying quantities, then just define several blocks (in your hook_block_info()) and call the same helper function as you are now. If there is a requirement to configure the block then you should use the hook_block_configure() API
Now the data you are passing into your theme function, you are extracting field data from the node object using: $item->field_image['und'][0]. A couple of things here: fields are provided by the field module which has an extensive API to retrieve field data. You should use field_get_items() instead, which will return an array of all items associated with that field for that entity (as fields can be multi-valued) and handles the language for you. The other thing is never use 'und' use the constant LANGUAGE_NONE.
$body = field_get_items('node', $item, 'body');
$field_image = field_get_items('node', $item, 'field_image');
$field_tags = field_get_items('node', $item, 'field_tags');
$data[$item->nid] = array(
'title' => $item->title,
'tags' => $field_tags ? $field_tags : FALSE,
'body' => $body ? $body[0] : FALSE,
'image' => $field_image ? $field_image[0] : FALSE,
'nid' => $item->nid,
'col-width' => $colWidth,
'alias' => drupal_get_path_alias('node/'.$item->nid)
);
Another problem is that you set the $block['content'] to the result of theme('website_actualites_output', ...). Now this function will return markup, Drupal has many more calls in the chain before we need to render any markup, and the problem with calling this now is that you can't mutate your data at any point now in the build process.
In order to call theme('website_actualites_output', ...); You need to have defined this theme function in a hook_theme() call, presumably you have done this in another module, which is perfectly fine but remember to add a dependency on that module in your module.info file. What you really want to be doing is just adding instructions to the Drupal build, not markup:
return array(
'#theme' => 'website_actualites_output',
'#title' => $static_title,
'#content' => $static_content,
'#data' => $data
);
So then we come to your template, you have a lot of logic in your template and they are only intended for displaying content with markup, with ideally no, or minimal computation if any. A theme function has a hook_preprocess() call before hook_process(), and then the variables are passed into the template.
You are using this theme function to loop over the $data variable to display markup - ideally a theme function would be just that markup with the variables already processed and passed into it:
website-actualites-output--child.tpl.php
<div class="col-sm-<?php print $col_width; ?>">
<?php if ($image): ?>
<div class="img">
<?php print render($image); ?>
</div>
<?php endif; ?>
<?php if ($title): ?>
<p><?php print render($title); ?></p>
<?php endif; ?>
</div>
In this example your best bet would be to have a second theme function website_actualites_output__child with the above template.
/**
* Implements hook_theme().
*/
function website_actualites_theme() {
$templates = drupal_get_path('module', 'website_actualites') . '/templates';
return array(
'website_actualites_output__child' => array(
'path' => $templates,
'template' => 'website-actualites-output--child',
'variables' => array(
'col_width' => 4, // Default col width.
'image' => NULL,
'title' => NULL,
),
),
);
}
Then preprocess your current theme function:
/**
* Implements hook_preprocess_HOOK().
*/
function website_actualites_preprocess_website_actualites_output($variables) {
$processed_output = array();
$data = $variables['data'];
foreach ($data as $row) {
$image = array(
'#theme' => 'image',
'#path' => file_create_url($row['image']['uri']),
'#alt' => $image['alt'],
'#attributes' => array('class' => array('img-responsive')),
);
$processed_output[] = array(
'#theme' => 'website_actualites_output__child',
'#col_width' => $row['col-width'],
'#image' => array(
'#theme' => 'link',
'#path' => 'node/' . $row['nid'],
'#text' => $image,
'#options' => array('HTML' => TRUE),
),
'#title' => array(
'#theme' => 'link',
'#path' => 'node/' . $row['nid'],
'#text' => $row['title'],
),
);
}
// Reassign the $data variable so that we can just render() it.
// var_dump($processed_output) to get a better idea what's going on here.
$variables['data'] = $processed_output;
}
Then all you need to do in your current template is to:
<?php if ($data): ?>
<?php print render($data); ?>
<?php endif; ?>
And you can wrap this is whatever markup you like.
The Drupal learning curve is high, but remember that if someone else were to maintain this code after you, they would expect that you adhere to Drupal coding standards and followed the process upon which Drupal is built.
Further reading: Render Arrays in Drupal 7
Hope this information helps and makes it a bit clearer.

Add Button on Custom Plugin in WordPress

I am working on custom plugin in WP.I have showed the data in table format.Now i am trying to add functionality to plugin.But my table missing header and footer just like normal data listing in WP like Pages and Posts etc.Also i added the new button link.Either this is the right way ? I want to load my page on button clickMy code is :
function extra_tablenav( $which ) {
if ( $which == "top" ){
//The code that goes before the table is here
echo '<h2>Letter Templates Add New</h2>';
}
}
function get_columns() {
return $columns= array(
'col_id'=>__('ID'),
'col_name'=>__('Name'),
'col_url'=>__('Url'),
'col_description'=>__('Description')
);
}
public function get_sortable_columns() {
return $sortable = array(
'col_id'=>'id',
'col_name'=>'name'
);
}
You can use the $_column_headers extended Property
this property is assigned automatically, must manually define it in their prepare_items() or __construct() methods.
like,
function prepare_items(){
$columns = array(
'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
'col_id' => 'ID',
'col_name' => 'Name',
'col_url' => 'URL',
'col_description' => 'Description',
);
$sortable_columns = array(
'col_id' => 'ID',
'col_name' => 'Name',
);
$hidden = array();
$this->_column_headers = array($columns, $hidden, $sortable);
}
function extra_tablenav( $which ) {
if ( $which == "top" ){
echo '<h2>Letter Templates Add New</h2>';
}
}
The Wordpress natively supports URLs like wp-admin/admin.php?page= you acess plugin pages like wp-admin/admin.php?page=mypage&tab=add-letter
And then in your code you just look at the GET and pull up the main page or a sub-page as needed.
like
if(isset($_GET['type']) && $_GET['type']=='new'){
include('add-letter.php');
}
check

How to give attribute values to each option in Yii dropDownList?

I have a question related to Yii DropDownList. How can we give some attribute values to each option. For Example I need to build a dropdownlist looks like below.
<select>
<option value="a" myAttribute="xyz">Calendar</option>
<option value="n" myAttribute="PQR">Shopping Cart</option>
<option value="c" myAttribute="ABC">CD</option>
<option value="d" myAttribute="HMN">Email</option>
</select>
values and myAttributes's value are coming from table. How can I implement this by altering the following?
<?php echo $form->dropDownList($model,'media_ids',Media::getAllmedia(),array('?'=>'?')); ?>
in model Media I have function like this
public function getAllmedia() {
$condition = 'isActive = "Y"';
$model = Media::model()->findAll(array('condition' => $condition));
return CHtml::listData($model, 'id', 'media_title');
}
EDIT: My Requirement is to show images along with each option, for that i have to give another attribute to each option which holds the image path
CHtml::dropDownList() not allow to do that..
Try creating a other function like this:
Media Class file:
static function getAllmediaDDL($idSelected=null) {
$condition = 'isActive = "Y"';
$model = Media::model()->findAll(array('condition' => $condition));
$combo = CHtml::tag('select', array('id' => 'mySelect'));
foreach ($model as $value){
$selected = $value->id == $idSelected? true : false;
$combo .= CHtml::tag('option', array('value' => $value->id, 'myattribute'=>$value->url_img, 'selected'=>$selected), CHtml::encode($value->media_title), true);
}
$combo .= CHtml::tag('select');
return $combo;
}
And in view call:
echo Media::getAllmediaDDL($model->id); // If you want to call in this way, you need to declare it as static function.
You have the answer in listOption details, and you have to implement another function like you did for options value to map each id which is the value with the image path: array(id=>array(image_path_attribute=>value), another_id=>(array(),...) for example lest assume that the function name is listImagePath() that return an array of options but dont forget that each value is mapped to array of attributes not just one
<?php echo $form->dropDownList($model,'media_ids',Media::getAllmedia(),array('options'=>Media::listImagePath())); ?>
It's work:
<?php
echo $form->dropDownList($model,'media_ids',
'data' => CHtml::listData(Media::model()->findAll(), 'id', 'name'),
array(
'options' => CHtml::listData(Media::model()->findAll(), 'id', 'option_array')
)
);
?>
And in model:
public function getOption_array() {
return array('myAttribute' => $this->media_title);
}
Im sorry im still a beginner but is this what you are looking for?
in your _form:
<?php echo $form->dropDownListRow($model, 'status',array(0=>"Inactive",1=>"Active")
); ?>

Undefined property - Symfony2

I'm using Symfony2. My controller finds some values - like categories, created by the and gives them to the template. The problem is that if the user hasn't any created categories yet, I want to display a massage to invite him to create categories.
Here is the code:
if($number_of_categories == 0){
$newcommer = true;
//Here the template doesn't need any variables, because it only displays
// "Please first add some categories"
} else {
$newcommer = false;
//Here the variables, which I give to the template
//are filled with meaningfull values
}
return $this->render('AcmeBudgetTrackerBundle:Home:index.html.twig', array(
'newcommer' => $newcommer,
'expenses' => $expenses_for_current_month,
'first_category' => $first_category,
'sum_for_current_month' => $sum_for_current_month,
'budget_for_current_month' => $budget_for_current_month
));
The problem is that if the user doesn't have categories I don't have with what to fill the variables, so I have to write something like this:
//What I want to avoid is this:
$expenses_for_current_month = null;
$first_category = null;
$sum_for_current_month = null;
$budget_for_current_month = null;
just to avoid Notice: Undefined variable ...
Is there a cleaner solution to achieve this? There isn't a way to dynamically generate the count of variables, given to the template, is there? Thanks in advance!
Here a simple solution (if I understood your problem) :
$template = 'AcmeBudgetTrackerBundle:Home:index.html.twig';
if ($number_of_categories == 0){
//Here the template doesn't need any variables, because it only displays
// "Please first add some categories"
return $this->render($template, array(
'newcommer' => true,
));
} else {
//Here the variables, which I give to the template
//are filled with meaningfull values
return $this->render($template, array(
'newcommer' => false,
'expenses' => $expenses_for_current_month,
'first_category' => $first_category,
'sum_for_current_month' => $sum_for_current_month,
'budget_for_current_month' => $budget_for_current_month
));
}
And if you want a cleaner solution to manage your Template, you cna use the annotation #Template() (You just have to return an array to pass to the view) :
// ...
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class MyController extends Controller
{
/**
* #Route("/my_route.html")
* #Template("AcmeBudgetTrackerBundle:Home:index.html.twig")
*/
public function indexAction()
{
// ...
if ($number_of_categories == 0){
return array(
'newcommer' => true,
);
} else {
//Here the variables, which I give to the template
//are filled with meaningfull values
return array(
'newcommer' => false,
'expenses' => $expenses_for_current_month,
'first_category' => $first_category,
'sum_for_current_month' => $sum_for_current_month,
'budget_for_current_month' => $budget_for_current_month
);
}
}
}

Theming forms on alter with Drupal 7

I'm having issues customizing a radio button for a specific form using hook_theme. Below is the piece of code I have on my module; see my comments elaborating the issue I encounter:
// Implementation of hook_form_alter().
function mymodule_form_alter(&$form, $form_state, $form_id){
// e.g form id: commerce_cart_add_to_cart_form_u6onPJSgS7pOgw0Tlo7zHy42LTQzbV913taANkYQKTo
if (strpos($form_id, 'commerce_cart_add_to_cart_form') !== FALSE) {
// Alter add to cart form
mymodule_commerce_cart_add_to_cart_form_alter($form, $form_state, $form_id);
}
}
function mymodule_commerce_cart_add_to_cart_form_alter(&$form, $form_state, $form_id) {
// Change the field type to radios.
$form['attributes']['field_artwork_ref']['#type'] = 'radios';
// Apply my custom theme for radios.
$form['attributes']['field_artwork_ref']['#theme'] = array('custom_radios');
}
// Implementation of hook_theme().
function mymodule_theme() {
return array(
'custom_radios' => array(
'variables' => array('element' => NULL),
),
);
}
function theme_custom_radios($variables) {
// Custom theme should go here.
// However, $variables are empty, print_r gives me "Array ( [element] => )."
// I am at least expecting to see my radio element here.
print_r($variables);
}
Themes for Drupal 7 form elements need to use the new render array key instead of variables in the theme definition:
function mymodule_theme() {
return array(
'custom_radios' => array(
'render element' => 'element',
),
);
}
Once you've made the change clear Drupal's caches and your code should work (I've just tested the above and it works fine).

Categories