How to get GET and POST parameters in view in Zend Framework 3? - zend-framework3

I'm using ZF3, I know the below works in controllers:
$this->params()->fromQuery('my_get_var', 'default_value');
$this->params()->fromPost('my_post_var', 'default_value');
How to get these in views? (of course, without accessing superglobals $_GET and $_POST directly)

I assume that it is the same as ZF2, passing them to the view.
$viewModel = new ViewModel();
$viewModel->my_get_var = $this->params()->fromQuery('my_get_var', 'default_value');
$viewModel->my_post_var = $this->params()->fromPost('my_post_var', 'default_value');
return $viewModel;
You can then display them in your view.phtml
<?php echo $this->my_get_var; ?>
<?php echo $this->my_post_var; ?>
See my answer here.

As an alternative way of returning and accessing you could use the provided "Zend magic".
In a Controller, return as so:
return [
'getName' => $this->params()->fromQuery('get_var', null),
'postName' => $this->params()->fromPost('post_var', null),
];
The magic here is that a ViewModel is automagically created for you. The second piece of magic is that the returned keys are set as variables in the newly created ViewModel.
(edit: just spotted this link provided by #Garry in his answer which already contains the above)
In the ViewModel you could also make use of some of the Zend Framework magic. You could use $getName and $postName instead of $this->getName/$this->postName. So, to print do:
<?= $getName ?: 'No GET params given' ?>
<?= $postName ?: 'No POST params given' ?>

Related

How to get data from array object in WordPress plugin api

http://localhost/wordpress/give-api/forms/?key=15443f18029e6f5d3b65d04e1640ffbe&token=c3de770a410282359413c74a588c5c74
The above link is a plugin api link. Above link won't work to your browser.
when I set the above link in the browser , it returns array object like http://postimg.org/image/6ozmjy0e7/ .
My question is , how can I set this url in a variable in wordpress and how can I get the data from that array object. I just want to get the data from that array object. If any other process is available, then please suggest me. Thanks...
In functions.php:
function displayApiUrl() {
global $apiUrl; // you probably don't actually need to set it global as it is a function
$apiUrl = 'http://localhost/wordpress/give-api/forms/?key=15443f18029e6f5d3b65d04e1640ffbe&token=c3de770a410282359413c74a588c5c74';
return $apiUrl;
}
In your theme you can now use:
<?php $api = displayApiUrl(); ?>
With that you can process your array in a foreach loop:
<?php
$json_url = file_get_contents($api);
$json_data = json_decode($json_url, true);
foreach ($json_data['forms'] as $form) {
$form_id = $form['info']['id'];
echo $form_id;
}
?>
The new "standard" for WordPress rest apis is Json Rest API, which will be partially integrated into WordPress core in the next release.
You can get it here https://wordpress.org/plugins/json-rest-api/ and documentation at http://wp-api.org/
In terms of the question how to put array information into the URL, the format is
http://www.example.com/wp-json/endpoint?array_1[key1]=Pensacola&array_1[key2]=Florida
The URL of course changes, and the wp-json/endpoint is replaced with whatever the final endpoint is for which ever rest api you choose to use.

WP Settings API save to multiple options

In the Wordpress Settings API, creating a new options page usually starts out with
register_setting('sample_options', 'my_option');
add_settings_section('section', 'Sample Options', 'callback1', 'page');
add_settings_field('name', 'Label', 'callback2', 'page', 'section');
In this simplified example, the data gets saved in the option my_option making the value of name accessible through
$option = get_option('my_option');
$name = $option['name']; // Got it
But what if the value of the name field is there not to place a new value but to update an already existing option that's not my_option like for example this_other_option? I guess what I'm really looking for is is it possible for one field to save to multiple options (my_option and this_other_option) while using the Settings API?
I suppose you could use a callback in register_setting. It would look something like the following:
<?php
register_setting('sample_options', 'my_option', 'my_sanitize_callback');
function my_sanitize_callback($value, $option) {
$value = mysql_real_escape_string($value);
update_option('my_other_option', $value);
return $value;
}
?>
You may have to tweak that a bit. I haven't tested it.

Drupal 7 - Why does $acc->value()->name print out the username?

<?php
$involved_users = array();
//grab usernames from user reference field of a node
$project = entity_metadata_wrapper('node', $node);
// field_users is user reference field
foreach ($project->field_users as $acc) {
$involved_users[] = $acc->value()->name;
}
var_dump($involved_users);
?>
Hi Guys,
I got this bit of code from http://pixeljets.com/blog/writing-robust-code-uses-fields-drupal-7
I have been going through it but I can't seem to figure out how "$acc->value()->name;" work?
What is value()? Is it PHP method or a Drupal one. I can't find any documentation on this.
Thanks!
If you read the blog post you provided carefully, you'll see that the value() method is provided by entity_metadata_wrapper() from the Entity module.

Add Meta Post function not working

I am using add post meta function to save some data and its not working
<?php
//include '../../../wp-blog-header.php';
$unique = "true";
$pageID = $_GET['postID'];
echo "pageID:";
echo $pageID;
echo "</br>";
$num_posts = $_GET['num_posts'];
echo "num_posts: ";
echo $num_posts;
echo "</br>";
$num_posts_meta_key = "num_posts";
add_post_meta($pageID, $num_posts_meta_key, $num_posts , $unique) or update_post_meta($pageID, "num_posts" , $num_posts);
?>
Can someone help me out?
In first page I am getting all values from textboxes or checkboxes in javascript and then i am passing it in URL to next page where add_post_meta function is there.
I tried using method POST ...but then it doesnt work for me. It just submit the page and come back w/o doing anything on 1st page. I tried with GET method..but nothing works.
Hence I decided to take all values like num of post, post id in javascript and then from there pass it with url by using window.location.
I am very new to wordpress plugin coding. I thought POST method in my plugin is conflicting with some other post method in post.php..not sure though..
I am writing plugin for admin panel.
not sure what your problem is.. are you sure you're passing the right postID parameter? does the post exist in the database?
You don't really need to do add_post_meta() or update_post_meta.
From the manual:
The first thing this function will do
is make sure that $meta_key already
exists on $post_id. If it does not,
add_post_meta($post_id, $meta_key,
$meta_value) is called instead and its
result is returned.
<?php
// This minimum code should work, though you should really check that a post
// with this id does exist.
update_post_meta($_GET['postID'], "num_posts" , $_GET['num_posts']);
?>

Drupal: Access $profile from a block

I'm trying to get the avatar (profile picture) located in the $profile array to appear in a BLOCK. The variable $profile is not accessible from blocks. It's scope is only in that actual user-profile.tpl.php file. So... does anybody know how I can execute something like this:
print $profile[user_picture];
in a drupal BLOCK?
I figured i might as well post it here as well. See my second comment on the first thread in this discussion. Below is my code I used with INSERT VIEW to get what I wanted:
<?php
$profileUser = "";
if (arg(0) == "user") {
$profileUser = arg(1);
}
// removed some other checks i do to populate $profileUser
?>
[view:VIEWED_PROFILE_AVATAR=block=<?php print $profileUser; ?>]
I hope that helps someone.
You can try using the following code in a new block (admin/build/block/add):
<?php
global $user;
$output = theme_image($user->picture, $alt = 'user pic', $title = 'user pic');
print $output;
This gives you access to the global $user variable and then you can use the picture property to get the URL for the current users profile picture.

Resources