<?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.
Related
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.
I'm new to Drupal. I looked here and on google for a while before asking, but I'm sure I can't find the answer because I don't know how to ask the question.
Here is what's going on. I'm using a custom module to load certain entities and then output them in a specific format for an application to access. The problem is that the NODE BODY contains special information and media files that should be converted. My goal is to obtain the HTML output that would normally be used on this field.
// Execute an EntityFieldQuery
$result = $query->execute();
if (isset($result['node'])) {
$article_items_nids = array_keys($result['node']);
$article_items = entity_load('node', $news_items_nids);
}
// Loop through each article
foreach ($article_items as $article) {
return $article->body[LANGUAGE_NONE]['0']['value'];
}
All of this works great. The only problem is that I get things like this in the output:
[[{"type":"media","view_mode":"media_original","fid":"283","attributes":{"alt":"","class":"media-image","data-thmr":"thmr_32","height":"400","width":"580"}}]]
or
*protoss_icon*
My goal is to find a way that these items are converted just like they are when these articles are viewed normally.
I've tried doing things such as:
render(field_view_field('node', $article, 'body'));
or
render($article->body[LANGUAGE_NONE]['0']['value']);
without success. Thanks for any help, I'm learning so I don't have a complete grasp of the process drupal uses to build output.
You can try something like this (this works only with nodes not with other custom entity types):
$node = node_load($nid);
$field = field_get_items('node', $node, 'your_field_name');
$output = field_view_value('node', $node, 'your_field_name', $field[$delta]);
the field_view_value returns a renderable array for a single field value. (from drupal api documentation)
I'm doing this to render a node and returning the output to an AJAX call and displaying a node inline on a page:
$node = node_load($nid);
$node_view = node_view($node);
echo drupal_render($node_view);
I need to be able to do the same things for a user profile...is it similar?
It's pretty similar, you can use the following functions:
$account = user_load($uid);
$account_view = user_view($account);
echo drupal_render($account_view);
EDIT
I changed the variable name to use $account instead of $user, just to eliminate the possibility of overwriting the global $user variable.
The Entity API module provide a generic function to render any entity: entity_view() that can also be used. For a user entity this should produce the same results than user_view().
$account = user_load($uid);
$account_view = entity_view('user', $account);
echo drupal_render($account_view);
I've created a simple module for displaying a flash game in a custom block by overwriting game_block_view() and game_block_info() in the sites/default/modules/game.module and it works ok.
I need however to pass user avatar and also gender and city (I've added the 2 mandatory fields to the registration form) through the FlashVars-parameter to the flash game in my block.
So I'm trying to overload the hook_user_load, because I suppose that this is the method where you add properties to the $user object after it has been initiated from the database (this probably happens when the user logins or alters his/her profile data?):
function game_user_load($users) {
global $user;
$uid = $user->uid;
$result = db_query('select filename from {file_managed} where uid=:uid', array(':uid' => array($uid)));
$avatar = $result->fetchField();
$users[$uid]->avatar = $avatar;
drupal_set_message("<pre>$uid: $avatar</pre>\n");
print_r($users);
}
Unfortunately I see no output produced by the last 2 lines above in the web page
What am I doing wrong?
Thank you!
Alex
The global user object does not go through hook_user_load(), see http://api.drupal.org/api/drupal/includes--session.inc/function/_drupal_session_read/7. Don't ask me why, that's just the way it is :)
When using user_load(), any added fields will automatically be loaded, you don't need custom code for that. You just need to know how to access them, which is a bit complicated.
Something like this should work:
global $user;
// $account is now a fully loaded user object.
$account = user_load($user->uid);
// Your field name is probably 'field_avatar'.
if ($avatar = field_get_items('user', $account, 'field_avatar')) {
dpm($avatar); // only works with devel.module, strongly suggested!
}
I would like to build a custom search module in Drupal 6 for searching through CCK. I need the user to search between his nodes (node.uid=x) and of a certain type (type='xyz'). I think I have to implement hook_search but I don't know where to put my filters. Can anyone help me?
You already accepted an answer (which is probably the best option for you), but there are a few other ways to accomplish this.
IIRC, the Custom Search module will work for what you want.
You can copy the stock hook_search function to a custom module and modify the query. You can do something like this:
// ...
case 'search':
// Build matching conditions
list($join1, $where1) = _db_rewrite_sql();
$arguments1 = array();
$conditions1 = 'n.status = 1';
// NEW BIT START
$allowed = array(
'content_type_1',
'content_type_2',
'content_type_3',
);
$types = array();
foreach ($allowed as $t) {
$types[] = "n.type = '%s'";
$arguments1[] = $t;
}
$conditions1 .= ' AND ('. implode(' OR ', $types) .')';
$keys = search_query_insert($keys, 'type');
// NEW BIT END
This replaces the bit that extracts the type from the actual query string.
You would have to add in the bit to restruct to a particular n.uid. I have been using this method lately, rather that Custom Search, because it simpler from the user's perspective.
HTH
You might try creating a Views with an exposed filter, it's the absolute easiest way to implementing your idea.
Also you can try use CCK Facets. But Views - of course simple.