Getting print_r from a Drupal variable - drupal

I have a Drupal variable, $view. I need to print out all of its values. I have tried:
dsm($view);
var_dump($view);
function hook_form_alter() {
$form['print'] = array('#value' => '<pre>'.print_r($view, 1).'</pre>');
}
All of these functions produce an output of Null, however. How can I get the value of the variable?

This is probably happening because the $view variable isn't within scope in the function hook_form_alter().

Use:
$view = views_get_current_view();
Then you can access, for example, the arguments of the view:
$arg0 = $view->args[0];

function MYMODULE_form_alter(&$form, &$form_state, &$form_id){
switch($form_id){
case 'MY_FORM':
$display_id = 'block_1';
$view = views_get_view('my_view_machine_name');
$view->set_display($display_id)
$view->set_items_per_page(0); // or other
$view->execute();
$result = $view->preview();
// Also you can use $view->result to get result as array
$form['print'] = $result;
break;
}
}

Related

Gravity Forms (WP): global variable from after_submission

Once a user submits the form, I am trying to grab the data in field 1, assign it to a variable and then in a different function I am trying to use that data.
The data is successfully going into the variable defined in the after_submission function as I was able to simply tell the site to break if a certain word was in the variable. It's now a matter of getting that same data from a different function.
add_action("gform_after_submission", "after_submission", 10, 1);
function after_submission($entry, $form){
$shopname = $entry["1"];
global $response;
$response = 'https://openapi.etsy.com/v2/shops/'. $shopname .'/listings/active?api_key=xxxxxxxxxxxxx )';
}
function dynamic_url(){
$apiurl = $response;
return $apiurl;
}
Redeclare the global variable in your second function:
add_action("gform_after_submission", "after_submission", 10, 1);
function after_submission($entry, $form){
$shopname = $entry["1"];
global $response;
$response = 'https://openapi.etsy.com/v2/shops/'. $shopname .'/listings/active?api_key=xxxxxxxxxxxxx )';
}
function dynamic_url(){
global $response;
$apiurl = $response;
return $apiurl;
}

SilverStripe translate fieldlabels

I simply use _t() to translate CMS Fields in a DataObject: TextField::create('Title', _t('cms.TitleField', 'Title'));. I thought translating $summary_fields was just as simple, but it's not.
Instead of trying to translate Fields and their accompanying summary_fields seperately, I believe I noticed a better way how these fields are translated using the function FieldLabels as used in SiteTree.
Is there way I can translate these both fields in one place (DRY principle) and apply to both easily by calling the var?
Yes I would certainly say the use of FieldLabels is for localisation / translation because of the comment "Localize fields (if possible)" here in the DataObject code...
public function summaryFields() {
$fields = $this->stat('summary_fields');
// if fields were passed in numeric array,
// convert to an associative array
if($fields && array_key_exists(0, $fields)) {
$fields = array_combine(array_values($fields), array_values($fields));
}
if (!$fields) {
$fields = array();
// try to scaffold a couple of usual suspects
if ($this->hasField('Name')) $fields['Name'] = 'Name';
if ($this->hasDatabaseField('Title')) $fields['Title'] = 'Title';
if ($this->hasField('Description')) $fields['Description'] = 'Description';
if ($this->hasField('FirstName')) $fields['FirstName'] = 'First Name';
}
$this->extend("updateSummaryFields", $fields);
// Final fail-over, just list ID field
if(!$fields) $fields['ID'] = 'ID';
// Localize fields (if possible)
foreach($this->fieldLabels(false) as $name => $label) {
// only attempt to localize if the label definition is the same as the field name.
// this will preserve any custom labels set in the summary_fields configuration
if(isset($fields[$name]) && $name === $fields[$name]) {
$fields[$name] = $label;
}
}
return $fields;
}

recursive function using joomla db object

I want to write a recursive function in joomla that get all the child level categories using a category id using joomla's jmodel's db object.Following is my code that I have written:
function getChildCategories($type){
$query = "SELECT id FROM #__cd_categories WHERE parent_id='$type'";
echo $query."<br/>";
$this->_db->setQuery($query);
$list = $this->_db->loadObjectList();
if ($this->_db->getErrorNum()) { echo $this->_db->stderr(); return false; }
foreach($list as $record){
$this->childCategories[]= $record->id;
echo $record->id."<br/>";
return $this->getChildCategories($record->id);
}
return true;
}
So now problem is that, in joomla we use $this->_db_setQuery method and $this->_db->loadObjectList method , so in recursive call the result set, I think it overwrite, I think because the object is same. So can any one tell the way that how to overcome this problem? If you can solve this by using loop even that would be also very helpful for me.
I also think that once values are assigned to $list variable then that over write shouldn't be problem.So seems strange.Please tell if some one can tell me the way to do it?
thanks in advance
I don't think the issue is with the database object getting overwritten. It has been a bit since I have been struggling with recursive functions but I think the issue is with assigning the $list variable.
Should you not be returning that variable instead of boolean true like this:
function getChildCategories($type) {
$query = "SELECT id FROM #__cd_categories WHERE parent_id='$type'";
$this->_db->setQuery($query);
$list = $this->_db->loadObjectList();
if ($this->_db->getErrorNum()) { echo $this->_db->stderr(); return false; }
if ($list) {
foreach($list as $record){
$list->childCategories = $this->getChildCategories($record->id);
}
return $list;
} else {
return;
}
}

how to print output in drupal hook_block

How to Print the output, if i written in WHILE SNIPPET in the function ,
Below is my snippet, i want print retrieved result ,
i tried echo ,
but we should not use echo in drupal, and drupal set message function for debug purpose ,
So how to print my output in this example ,
function node_example_block($op='list',$delta=0){
switch($op){
case "list":
$block[0]['info'] = t('THIS IS EXAMPLE NODE EXAMPLE ');
return $block;
case "view":
$block['subject'] = "THIS MY FIRST SAMPLE BLOCK";
$block['content'] = drupal_get_form('display_node_title');
return $block;
}
}
function display_node_title(){
$result = db_query("SELECT * FROM node");
$output = '';
while ($obj = db_fetch_object ($result)){
$output .= $obj->title;
}
//drupal_set_message($output);
}
You're having display_node_title get passed through drupal_get_form, but display_node_title isn't a form function. If it were, it'd be constructing a $form array via the Form API and return $form; at the end.
Change:
$block['content'] = drupal_get_form('display_node_title');
to:
$block['content'] = display_node_title();
and add:
return $output;
to the end of your display_node_title() function.

how to do boolean search in apache solr

I was trying to do do a boolean search 'sname:'.$user->name.' OR sname:xxxxxx; , i am not getting any results where as sname:xxxxxx; works fine. i even added mm=1 with the query modify hook. can somebody guide me how i can accomplish this.
Here is my code.....
$keys = "";
$filters = 'sname:'.$user->name.' OR sname:xxxxxx;
//print($filters);
$solrsort = variable_get('apachesolr_search_taxonomy_sort', 'created desc');
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$_GET['retain-filters'] = 1;
try {
//stolen from search.module (search_data)
$data = apachesolr_search_execute($keys, $filters, $solrsort, 'search/apachesolr_search', $page);
$results = theme('search_results', $data, $type);
} catch (Exception $e){
watchdog('apachesolr', t('Error running search'));
}
function reports_apachesolr_modify_query(&$query, &$params, $caller) {
// I only want to see articles by the admin!
$params['mm'] = 1;
}
Adv thanks.
The $user object is not available in some places, so it depends where you are calling it from. You need to invoke it globally. Add the following to the top of your code.
$keys = "";
global $user; //Add this line
$filters = 'sname:'.$user->name.' OR sname:xxxxxx';
Also note that you are missing a closing quotation mark after the xxxxxx. I added it in the above code.
Did you try removing the second sname:?? My SOLR filters look like:
(fname:carrie OR carol)
AND
(lname:miller OR jones)

Resources