Drupal: use view output as node content - php

I'm writing a Drupal 7 module to display child nodes of a module in the content.
Nodes have a field parent_nodes (node reference) where one ore more nodes are selected as parents.
First, I've created a view projects with a block view display subprojects, displaying nodes of type project with a field_parent_project contextual filter.
This is my module:
<?php
function projects_preprocess_node(&$variables) {
if ($variables['type'] == 'project') {
if (isset($variables['view_mode']) && $variables['view_mode'] == 'full') {
_projects_add_subprojects($variables);
}
}
}
function _projects_add_subprojects(&$variables) {
$nid = $variables['nid'];
$view = views_get_view('projects');
$preview = $view->preview('subprojects', array($nid));
$subprojects = array(
'#title' => t('Subprojects'),
'#label_display' => 'above',
'#weight' => 10,
//'#theme' => 'field',
'#markup' => $preview,
);
if (!isset($variables['content']['subprojects'])) {
$variables['content']['subprojects'] = array();
}
$variables['content']['subprojects'][] = $subprojects;
dpm($variables['content']);
}
This is working, adding the view display output to the node's content.
Only some things aren't working:
title (label)
weight do not change display position when rendered with other contents (it's always the first, above body).
If I uncomment the '#theme' => 'field' line, title is shown as a label, but nothing is rendered. This is because the field theme is used and I guess it needs #items and does not use the #markup element.
I cannot use a children nodes as reference, but only parent nodes.
The solution must be independent to theme, so no not answer "change your theme template" or similar
How can I show children nodes in node? I'm looking for a way to get something interpretable how a it is was a field

If you didn't already know, the Viewfield module allows you to specify a View as a field in your content type. This may save you some coding but you may not want a whole module to do such a specific task so...
If you want to continue with the custom code which you've written, then you need to re-structure your added content to the correct render array structure that Drupal expects. Try something like this:
$subprojects_view_output = array(
'#type' => 'markup',
'#markup' => $preview,
);
$subprojects = array(
'#theme' => 'field',
'#weight' => 10,
'#title' => t('Subprojects'),
'#items' => $subprojects_view_output,
);
With the above, the title/label for your field as well as the content of the field (the view itself) should show up. The code is untested so may not be 100% correct in terms of syntax and all but hopefully gives you a path to a solution.
EDIT: I tested the above and it does not work because in order to use the existing theme_field function it seems that Drupal expects more information required to render a field like the #field_name, #field_type, #entity_type, etc. as you should see in the warning messages.
Essentially, you are faking a field and you will need to provide Drupal with all the info it expects if you want to continue to use the built-in theme_field function, including all the variables as expected in the preprocess functions.
Alternatively, you can continue to use your original code and add a #prefix to get your title/label to render like this:
$subprojects = array(
'#weight' => 10,
'#prefix' => '<div id="subprojects-view">asdf:</div>',
//'#theme' => 'field',
'#markup' => $preview,
);
Then style the title/label with CSS accordingly. I didn't have any problems with the weighting as you described.

Thanks to #nmc, this is my final solution. If no results are found, it does not display the title. The check for results is done by if (count($view->result) == 0). Weight is working.
<?php
function projects_preprocess_node(&$variables) {
$type = $variables['type'];
if ($type == 'project' || $type == 'customer') {
if (isset($variables['view_mode']) && $variables['view_mode'] == 'full') {
_projects_add_subprojects_markup($variables);
}
}
}
function _projects_add_subprojects_markup(&$variables) {
$nid = $variables['nid'];
$view = views_get_view('projects');
$preview = $view->preview('subprojects', array($nid));
if (count($view->result) == 0) {
return;
}
$variables['content']['subprojects'] = array(
'#weight' => 10,
'#prefix' => '<h2>' . t('Subprojects') . '</h2>',
'#markup' => $preview,
);
}

Related

Drupal 8 form element with #autocreate taxonomy term

I made module with form, that use autocomplete field like that:
$form['field_taxonomy_tags'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'taxonomy_term',
'#selection_settings' => [
'target_bundles' => array('tags'),
],
'#autocreate' => array(
'target_bundles' => array('tags'),
'bundle' => ('tags'),
),
'#title' => ('tags'),
'#tags' => TRUE,
];
Autocomplete works fine, and i can add taxonomy terms from tags vocabulary easily. But there is some problem with #autocreate option i think. Have searched all documentation, and code inside drupal core. Entity is never created ;/
When i try to get value from this field, my browser is dead... there is some entity type variable, but huge.
After some debugging i found way to get it work, but im not happy about it :) Very strange, maybe some of you guys can help me to find better way?
public function submitForm(array &$form, FormStateInterface $form_state) {
$tags = $form_state->getValue('field_taxonomy_tags');
foreach ($tags as $tag)
{
if(is_object($tag['entity']))
{
$tag['entity']->save();
}
}
}
As you can see, I need to save those tags manually, dont know why ;/ Without it, there is no term created.
It is better way. I dont need to save every tag, its enough if we attach them to a node. Its entity object, that can be passed as node value, and after that, all tags will be created:
$node = Node::create(array(
'type' => 'YOUR_content_type',
'title' => $form_state->getValue('title')
));
$fieldNames = array_keys($node->getFieldDefinitions());
$values = $form_state->getValues();
// be aware with that, i use this loop for testing because i have same names
// you can use $node->set('content type field name', $value); directly without any field definitions
foreach ($values as $key=>$value)
{
if(in_array($key, $fieldNames))
{
$node->set($key, $value);
}
}
// here we save all data, taxonomy entities too
$node->save();

How to return a variable operator like $option[];

Obviously, I can't do this, but is there some way to achieve what I am trying to? I only found can not do's online, but no potential workarounds.
Here is what I am trying to do.
Currently I get the following error... "Cannot use [] for reading"
For my theme, I have a framework and the fields from that framework are built using an array that I create.
It looks something like this (minus the 300+ lines of code that I actually use)...
$options[] =
array(
'title' => 'This Field Tab Title',
'name' => 'this-field-tab-slug',
'fields' =>
array(
// ----------------------------------------------------------------------
// This Field Option Name
// ----------------------------------------------------------------------
array(
'type' => 'this_field_type',
'id' => 'this_field_types_id',
),
// ----------------------------------------------------------------------
// This Field Option Name
// ----------------------------------------------------------------------
array(
'type' => 'this_field_type',
'id' => 'this_field_types_id',
),
// ----------------------------------------------------------------------
// This Field Option Name
// ----------------------------------------------------------------------
array(
'type' => 'this_field_type',
'id' => 'this_field_types_id',
),
),
);
I am running a grouped field type, so my output has many options/fields within this grouped field/area which can then be added again and again as many times as the user needs. Then I am repeating that whole process/code again but for other taxonomies of the user's site.
So for example, the whole process above applies to post types, categories, tags, archived, etc. etc. So instead of having thousands of lines of repetitive codes, I'm trying to create my own function and pass the variables to that function.
But for the function, I find I can't return $options[];
Here is a screenshot of what I mean by the grouped field that can be added as many times as the user needs.
And here's an example of the function I am trying to create!
public static function layout_settings_config($title_name = '', $title_slug = '', $title_id = '', $query = '') {
$title_name = 'Post Type';
$title_slug = 'post-type';
$title_id = 'post_type';
$query = 'post_types';
$options[] =
array(
// all the config array codes in here...
);
return $options ??? $options[]; doesn't work.
}
Is this possible to achieve what I am trying to a different way? I'm still a little new to creating my own functions and OOP, but nothing I find online for this specific issue with a workaround.
Thanks!
$options[] is not object but it is an operation like function.
You should return $options instead.
and, by the way, when you say $options[] = something. it actually insert something inside an array called $option. so effectively you have to access your options like this.
$option[0]->title.
So I suggest Instead of making it complex like this. simply say
$option = something.

Putting correct php-code within eval()-string

I am working on a Wordpress project where I need to dynamically create a function (depending on which kind of template is used for a page or post) that retrieves the comments of each page in question.
So let's say I have pages within Wordpress with the IDs 100, 110, 120, 130, 140, 150 and out of these 3 are using the template called "blog" (e.g.: 100, 130 and 150).
So in order to retrieve the comments from these 3 pages with AJAX I need to create a function for each of them:
function GetComments100() { #### }
function GetComments130() { #### }
function GetComments150() { #### }
Here's the function code I need to create individually for each page (and which goes in between the function brackets above (instead of the ####):
$defaults = array( 'order' => 'DESC', 'post_id' => $functionID, 'post_type' => 'page', 'count' => false );
$comments = get_comments($defaults);
foreach($comments as $comment) :
echo "<div class='table-row' style='margin-bottom:1px'><div class='table-cell-1' style='width:110px;'>".$comment->comment_author.":</div><div class='table-cell-2'style='width:870px;'>".$comment->comment_content." <em><a>".$comment->comment_date." ... ".get_the_title($comment->comment_post_ID)." (".$comment->comment_post_ID.")</a></em></div></div>";
endforeach;
die($results);
In order to get the pages I use a loop-function which gives me the page ID as a variable (in my case its $functionID (also included in the array of my function above)).
I have already managed to dynamically create the functions with the following lines of code (I know "eval" is not a good choice but I didn't find any other solution):
$string = 'function ' . $functionName . "() {
####
}";
eval($string);
Now instead of the #### I need to integrate the actual function code starting with "$defaults = array(..." but obviously it has to be completely converted to a string - which is what I am struggling with.
Any help would be appreciated (again, I know using "eval" is not nice but so far I didn't find any other solution for this)
Have you tried using nowdoc for the function body? If you just need expanding $functionName, you can try something like this:
$string="function {$functionName}(){".<<<'END'
$defaults = array( 'order' => 'DESC', 'post_id' => $functionID, 'post_type' => 'page', 'count' => false );
$comments = get_comments($defaults);
foreach($comments as $comment) :
echo "<div class='table-row' style='margin-bottom:1px'><div class='table-cell-1' style='width:110px;'>".$comment->comment_author.":</div><div class='table-cell-2'style='width:870px;'>".$comment->comment_content." <em><a>".$comment->comment_date." ... ".get_the_title($comment->comment_post_ID)." (".$comment->comment_post_ID.")</a></em></div></div>";
endforeach;
die($results);
}
END;
eval($string);
I don't understand why you don't use one function per Template with a parameter like this:
public function getBlogComments($id){
//...
}
or one function which check the used Template
public function getComments($id){
// get Template of $id
//...
}

Drupal: how to show a form (select) acting as filter options, then show a table of stuff from the database?

I want a simple filter form, and a table below it. When the user changes the option on the select form, the table automaticaly changes. I think thats done with ahah.
I want this (some things can change, like the fieldset containing the table, and other stuff):
But working.. of course..
I'm currently showing that page using one function. It's a complete mess and something like "NEVER DO THIS", but i'm researching and trying some stuff as i'm a drupal learner.
This is the relevant code:
form = array();
ahah_helper_register($form, $form_state);
//query here, build $options for the select
$form['listar_veics'] = array(
'#type' => 'fieldset',
'#prefix' => '<div id="listar-veics-wrapper">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
if (!isset($form_state['values']['listar_veics']['filial']))
$choice = 1;
else
$choice = $form_state['values']['listar_veics']['filial'];
$form['listar_veics']['filial'] = array(
'#type' => 'select',
'#title' => "Listar veĆ­culos da filial",
'#options' => $filiais,
'#default_value' => $choice,
'#ahah' => array(
'event' => 'change',
'path' => ahah_helper_path(array('listar_veics')),
'wrapper' => 'listar-veics-wrapper',
'method' => 'replace',
),
);
//query for the rows i wanna show
//building $data array, the rows array
//building $header, as an array of strings
$table = theme_table($header, $data);
$page = drupal_render($form);
$page .= $table;
return $page;
So in this code, drupal will only replace the form itself, when i change the option on the select, it shows the new value on the select, but the table isnt rendered again thus not changing.
Thanks, apreciate every suggestion.
I recommend looking at the views module.
http://drupal.org/project/views
With that installed, you can create a custom query.
Add all the fields that you would like displayed on your table. (Placa, Marca)
Then add any filters that you would like to limit your query by. (Listar veiculos da filial)
When adding the filter you "expose" it so that when looking at the query, you can change the options on the page.
Then if you want to view what you just made, you need to add a display. If you make it a page display, you can set the path directly to the table.
For more information might I recommend http://gotdrupal.com/videos/drupal-views-tutorial.
If you want neater exposed fields I might also recommend http://drupal.org/project/better_exposed_filters
To expand Asimov's answer here is a code example (for Drupal 7) that shows a taxonomy term filter for selecting nodes. The selected terms are stored in the session and used in the query to filter the results.
You can put it in a custom module. It doesnt require Views or any other contributed modules.
In the example code below the name of the custom module is tic . Rename tic to the name of your custom module.
Four elements are needed:
A function that outputs the filter and fetches and outputs the results
The filter form
A custom submit function that stores the chosen filter options in the session
A reset function that clears the session
Use hook_menu() to call tic_fetch_results().
Fetch, filter, output results
This example uses a dynamic query because it is easy to extend with conditions.
/**
* Filters, fetches and outputs results
*/
function tic_fetch_results() {
// Adds filter form to the build array.
$form = drupal_get_form('tic_term_filter_form');
$output = drupal_render($form);
$node_types = array('article', 'page', 'blog_post');
// Sets up dynamic query
$query = db_select('node', 'n')
->extend('PagerDefault')
->limit(33)
->fields('n', array('nid', 'title'))
->condition('n.type', $node_types, 'IN')
->condition('n.status', 1);
// Fetches selected values from session and applies them to the query.
if (isset($_SESSION['form_values']['terms']) && count($_SESSION['form_values']['terms']) > 0) {
$query->join('field_data_field_tags', 'tags', 'n.nid = tags.entity_id');
$query->condition('tags.field_tags_tid', $_SESSION['form_values']['terms'], 'IN');
$query->condition('tags.bundle', $node_types, 'IN');
}
$result = $query->execute();
$items = array();
foreach ($result as $row) {
$items[] = array('data' => $row->nid . ' - ' . $row->title);
// do something interesting with the results
}
$output .= theme('item_list', array('items' => $items, 'title' => '', 'type' => 'ul', 'attributes' => array()));
$output .= theme('pager');
return $output;
}
Construct the form
The taxonomy terms options list is populated from the Vocabulary tags
/**
* Implements hook_form().
*/
function tic_term_filter_form($form, &$form_state) {
// Loads terms from the Tags vocabulary and use as select options.
$vocab = taxonomy_vocabulary_machine_name_load('tags');
$terms = taxonomy_get_tree($vocab->vid);
$term_options = array();
foreach ($terms as $term) {
$term_options[$term->tid] = $term->name;
}
// Sets the values that are stored in session as default.
$storage = (isset($_SESSION['form_values']) ? $_SESSION['form_values'] : 0);
$selected_terms = isset($storage['tags']) ? $storage['tags'] : NULL;
$form['terms'] = array(
'#title' => 'Filter by terms',
'#type' => 'select',
'#options' => $term_options,
'#multiple' => TRUE,
'#default_value' => $selected_terms,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Filter'),
);
$form['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset'),
'#weight' => 30,
'#submit' => array('tic_tools_reset'),
);
return $form;
}
Store selected values in the session
/**
* Implements hook_form_submit().
*/
function tic_term_filter_form_submit(&$form, &$form_state) {
// Stores form values in session.
$_SESSION['form_values'] = $form_state['values'];
}
Reset the filter
/*
* Clears set filters.
*/
function tic_tools_reset() {
if (isset($_SESSION['form_values'])) {
unset($_SESSION['form_values']);
}
drupal_goto(current_path());
drupal_set_message('Filters were reset');
}
The following page on the drupal forum contains a very clear explanation of the drupal form process and what to do in your specific case. See the answer from user Jaypan.
http://drupal.org/node/1770512
To sum his answer up:
Create a submit button on the form to submit the chosen filter. This button has it's own submit function:
$form['submit_filter'] = array(
'#type' => 'submit',
'#value' => 'Apply the filter',
'#submit' => array('apply_filter')
);
Create the submit function for applying your filter. In this function store the value of the filter to save it for the next form building when the page refreshes. Also set $form_state['rebuild'] to TRUE.
function apply_filter($form, &$form_state)
{
// Save the filter
$form_state['filter'] = $form_state['values']['filter'];
$form_state['rebuild'] = TRUE;
}
Now you will have access to the filter value the next time the form is built. Just check for the existence of the value like:
if (isset($form_state['filter']))
{
// do your filtering here
}
Hope this helps.

How do I correctly create a Zend Feed?

I have successfully created a simple RSS feed, but entries keep coming back as unread and updated, and entries deleted from the client reappear everytime I ask mail to update the feed.
What am I doing wrong?
I use this simple function to create an rss feed:
public static function getFeed($db)
{
$title = 'Latest feeds';
$feedUri = '/rss/';
//link from which feed is available
$link = 'http://' . $_SERVER['HTTP_HOST'] . $feedUri;
//create array according to structure defined in Zend_Feed documentation
$feedArr = array('title' => $title,
'link' => $link,
'description' => $title,
'language' => 'en-us',
'charset' => 'utf-8',
//'published' => 1237281011,
'generator' => 'Zend Framework Zend_Feed',
'entries' => array()
);
$itemObjs = array();
$select = $db->select('id')->from('things')
->order('createddate desc')
->limit(10);
$results = $db->fetchAll($select->__toString());
$count = count($results);
for($i=0;$i<$count;$i++) {
$itemObjs[] = SiteUtil::getItemObjectInstance($db, $results[$i]['id']);
}
$count = count($itemObjs);
for($i=0;$i<$count;$i++) {
$obj = & $itemObjs[$i];
$feedArr['entries'][] = array('title' => $obj->getSummary(),
'link' => 'http://' . $_SERVER['HTTP_HOST'] . $obj->getDetailUri(),
'description' => $obj->description,
'publishdate' => $obj->publishedDate,
'guid' => 'http://' . $_SERVER['HTTP_HOST'] . $obj->getDetailUri()
);
}
$feed = Zend_Feed::importArray($feedArr, 'rss');
return $feed;
}
The action in the controller class is:
public function rssAction()
{
$feed = FeedUtil::getFeed($this->db);
$feed->send();
}
So to access the feed, I point the client to:
http://mysite.com/rss
I am using mac mail's rss client to test. The feed downloads just fine, showing all 5 items I have in the database for testing purposes. The problems are as follows:
1) If I mark one or more items as 'read' and then tell mail to update the feed, it pulls all items again as if I never downloaded them in the first place.
2) If I delete one or more items they come back again, unread, again as if it were the first time I subscribed to the feed.
3) Feeds are always marked as updated. Is that supposed to be the case?
Is is something to do with the parameters I'm setting, am I omitting something, or could the solution be something more subtle like setting HTTP content headers (e.g. '304 Not Modified')?
My understanding of rss is that once an item has been marked as read or deleted from the client, it should never come back, which is the behaviour I'm after.
Just to note, the 'link' and 'guid' parameters are always unique, and I have tried experimenting with 'published' and 'publishdate' (both optional) attributes only get the same result. The above code is a simplified version of what I have, showing only the relevant bits, and finally, yes, I have read the rss specification.
Thanks in advance for any help offered here, I'll be happy to clarify any point.
According to the Zend Framework Doc, you must use the lastUpdate parameter to set the last modification date of an entry.
'entries' => array(
array(
[...]
'lastUpdate' => 'timestamp of the publication date', // optional
[...]
So published for the feed, and lastUpdate for the entries.

Categories