When you get a node, how do you load the previous version (revision)?
I know how loading a revision but not how getting the previous revision number ($node->vid is the current revision).
thanks
Supposing that you have a node object $node, you can use the following code to get the previous revision.
$previous_vid = db_result(
db_query('SELECT MAX(vid) AS vid FROM {node_revisions} WHERE vid < %d AND nid = %d', $node->vid, $node->nid)
);
Once you have the previous revision, you can load the new node object with node_load(array('nid' => $node-nid, 'vid' => $previous_vid)).
The code should check if db_result() returns FALSE, in the case there isn't a previous revision.
To note that the field vid is global for each node; it doesn't contain the same value for different nodes.
Thanks all.
I found also an other solution:
$revisions = node_revision_list($node);
next($revisions);
if ($preview_key = key($revisions)) {
$preview_revision = $revisions[$preview_key];
$old_node = node_load($node->nid, $preview_revision->vid);
}
But if you have a lot of revision you get a big array.
If I understand what you're trying to do; you want to get the preview of the node after someone submits changes?
The preview button has its own submit handler, node_form_build_preview(). There, it creates a new node object using the data in $form_state and runs node_preview(), which returns the markup for the preview.
If you want to capture that preview when the user clicks the preview button, you'll need to use hook_form_alter to add another submit handler to the preview button:
$['form']['buttons']['preview']['#submit'][] = 'mymodule_custom_preview';
where mymodule_custom_preview is the name of your custom submit function. Take a look at node_form_build_preview() for guidance, but your submit function is going to look something like this:
function mymodule_custom_preview($form, &$form_state) {
$node = node_form_submit_build_node($form, $form_state);
$preview = node_preview($node);
}
Also take a look at node_form(), which gives you an idea of how the node form is structured. When you're all done, you're going to have code in your module that looks something like this:
function mymodule_form_alter(&$form, $form_state, $form_id) {
if (strstr($form_id, '_node_form') !== FALSE) {
$['form']['buttons']['preview']['#submit'][] = 'mymodule_custom_preview';
}
}
function mymodule_custom_preview($form, &$form_state) {
$node = node_form_submit_build_node($form, $form_state);
$preview = node_preview($node);
// Do what you will with $preview.
}
Related
I'm trying to add a preprocess hook to add classes based on taxonomy name to the body css of my drupal installation. I've managed to get all the information about the node based on doing some searching around and trial and error, but I'd like to take it to the next step and get all the taxonomy terms based on the particular node id.
My current preprocess code is follows:
function custom_preprocess_html(&$variables) {
// Add the node ID and node type to the body class
$body_classes = [];
$nodeFields =\Drupal::service('current_route_match')->getParameter('node')->toArray();
if (is_array($nodeFields) && count($nodeFields) > 0) {
if (isset($nodeFields['nid'])) {
$body_classes[] = 'node-' . $nodeFields['nid'][0]['value'];
}
if (isset($nodeFields['type'])) {
$body_classes[] = $nodeFields['type'][0]['target_id'];
}
}
$variables['attributes']['class'] = $body_classes;
}
It works fine and pulls down the information regarding the node. Based on the answer here it seems like all I should have to do is add the following line to get the taxonomy terms: $taxonomyTerms = $nodefields->get('field_yourfield')->referencedEntities(); but when I do so Drupal throws an error. I'll freely admit I'm new to Drupal 8, so any suggestions about where I'm going wrong (is field_yourfield not something that exists, maybe?) would be greatly appreciated.
If you are trying to get the referenced term names and add them as body classes, your approach seems a little bit off.
This is what I use:
function CUSTOM_preprocess_html(&$variables) {
// Entity reference field name.
$field_name = 'field_tags';
// Get the node object from the visited page.
// If the page is not a node detail page, it'll return NULL.
$node = \Drupal::request()->attributes->get('node');
// Let's make sure the node has the field.
if ($node && $node->hasField($field_name)) {
$referenced_entities = $node->get($field_name)->referencedEntities();
foreach ($referenced_entities as $term) {
$variables['attributes']['class'][] = \Drupal\Component\Utility\Html::cleanCssIdentifier($term->getName());
}
}
}
I have an old SS2.4 site which I have updated to SS3.1.13. The only part of the old site I can't get working is a search form that filters DataObjects. The old code is:
public function doCollectionSearch($data, $form)
{
$filters = array();
...
//setup some filters based on user selections
...
$where = implode(" AND ", $filters);
if(!isset($_REQUEST['start'])) $_REQUEST['start'] = 0;
$limit = $_REQUEST['start'].",50";
return $this->customise(array(
'Collections' => DataObject::get("Collection", $where, "Genus, Species ASC", null, $limit)
))->renderWith(array('Collection_results','Page'));
}
I have updated the last part to:
return $this->customise(array(
'Collections' => Collection::get()->where($where)->sort("Genus, Species ASC")->limit($limit)
))->renderWith(array('Collection_results','Page'));
But I get a "the method 'fortemplate' does not exist on 'CollectionPage_Controller'".
I know the $where is not right yet, but if I strip that out I still get an error..
I know I am missing something obvious...can anyone suggest a fix?
You'll get that error if your template accesses a method or database field that returns an object that can't be reduced to a string. This often happens when you have a related object (say a parent page) and you do something like the following:
<p>My parent is: $Parent</p>
Instead of
<p>My parent is: $Parent.Title</p>
The same thing existed in 2.4 so this doesn't totally explain your problem, but I'd look for something like the above scenario in your templates.
What I am trying to do is to enable/disable revision according to the selected taxonomy term in the content type that I have created i.e. when a user add the content the user can select the taxonomy term field(may be a select field) according to the selected option I want to enable/disable the revision. How can I do this?
Turn off the create new revision setting for the content type.
Then in hook_form_alter add a new submission handler before the main one:
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
//drupal_set_message("Form ID is : " . $form_id);
switch($form_id) {
case 'CONTENT_TYPE_node_form':
//dpm($form);
$form['actions']['submit']['#submit'][] = 'revision_control_node_form_submit';
$form['actions']['submit']['#submit'] = array_reverse($form['actions']['submit']['#submit']); // reverse array to put our submit handler first
break;
}
}
Then in the new submit handler check if the taxonomy term has the correct value to save a new revision. I've not tried this next bit but according to this page putting
$node->revision = 1;
before node save will create a new revision.
node_save is called in node_form_submit and the node object is built in node_form_submit_build_node.
Looking at the other attributes like vid that belong to $form_state I would say an good educated guess would be to put $form_state->revision = 1; and see if that comes out as a property of the node after node_form_submit_build_node.
So you final new submit handler will look something like:
function revision_control_node_form_submit($form, &$form_state) {
if($form_state['values']['your_taxonomy_field'] == 'your_value') {
$form_state->revision = 1;
}
}
Now I've not actually tried any of this but even if it doesn't work I'm sure you will be on the right track... Good luck!
I am trying to modify a field collection in a node that already exists so I can change an image on the first element in an array of 3. The problem is, the hostEntity info is not set when I do a entity_load or entity_load_single so when I do a:
$field_collection_item->save(true); // with or without the true
// OR
$fc_wrapper->save(true); // with or without the true
I get the following error:
Exception: Unable to save a field collection item without a valid reference to a host entity. in FieldCollectionItemEntity->save()
When i print_r the field collection entity the hostEntity:protected fields are indeed empty. My field collection is setup as follows:
field_home_experts
Expert Image <--- Want to change this data only and keep the rest below
field_expert_image
Image
Expert Name
field_expert_name
Text
Expert Title
field_expert_title
Text
Here is the code I am trying to use to modify the existing nodes field collection:
$node = getNode(1352); // Get the node I want to modify
// There can be up to 3 experts, and I want to modify the image of the first expert
$updateItem = $node->field_home_experts[LANGUAGE_NONE][0];
if ($updateItem) { // Updating
// Grab the field collection that currently exists in the 0 spot
$fc_item = reset(entity_load('field_collection_item', array($updateItem)));
// Wrap the field collection entity in the field API wrapper
$fc_wrapper = entity_metadata_wrapper('field_collection_item', $fc_item);
// Set the new image in place of the current
$fc_wrapper->field_expert_image->set((array)file_load(4316));
// Save the field collection
$fc_wrapper->save(true);
// Save the node with the new field collection (not sure this is needed)
node_save($node);
}
Any help would be greatly appreciated, I am still quite new to Drupal as a whole (end-user or developer)
Alright so I think I have figured this out, I wrote up a function that will set a field collection values:
// $node: (obj) node object returned from node_load()
// $collection: (string) can be found in drupal admin interface:
// structure > field collections > field name
// $fields: (array) see usage below
// $index: (int) the index to the element you wish to edit
function updateFieldCollection($node, $collection, $fields = Array(), $index = 0) {
if ($node && $collection && !empty($fields)) {
// Get the field collection ID
$eid = $node->{$collection}[LANGUAGE_NONE][$index]['value'];
// Load the field collection with the ID from above
$entity = entity_load_single('field_collection_item', array($eid));
// Wrap the loaded field collection which makes setting/getting much easier
$node_wrapper = entity_metadata_wrapper('field_collection_item', $entity);
// Loop through our fields and set the values
foreach ($fields as $field => $data) {
$node_wrapper->{$field}->set($data);
}
// Once we have added all the values we wish to change then we need to
// save. This will modify the node and does not require node_save() so
// at this point be sure it is all correct as this will save directly
// to a published node
$node_wrapper->save(true);
}
}
USAGE:
// id of the node you wish to modify
$node = node_load(123);
// Call our function with the node to modify, the field collection machine name
// and an array setup as collection_field_name => value_you_want_to_set
// collection_field_name can be found in the admin interface:
// structure > field collections > manage fields
updateFieldCollection(
$node,
'field_home_experts',
array (
'field_expert_image' => (array)file_load(582), // Loads up an existing image
'field_expert_name' => 'Some Guy',
'field_expert_title' => 'Some Title',
)
);
Hope this helps someone else as I spent a whole day trying to get this to work (hopefully I won't be a noob forever in Drupal7). There may be an issue getting formatted text to set() properly but I am not sure what that is at this time, so just keep that in mind (if you have a field that has a format of filtered_html for example, not sure that will set correctly without doing something else).
Good luck!
Jake
I was still getting the error, mentioned in the question, after using the above function.
This is what worked for me:
function updateFieldCollection($node, $collection, $fields = Array(), $index = 0) {
$eid = $node->{$collection}[LANGUAGE_NONE][$index]['value'];
$fc_item = entity_load('field_collection_item', array($eid));
foreach ($fields as $field => $data) {
$fc_item[$eid]->{$field}[LANGUAGE_NONE][0]['value'] = $data;
}
$fc_item[$eid]->save(TRUE);
}
I hope this helps someone as it took me quite some time to get this working.
I've a custom module that builds a form with a couple of fields, so far so good.
In one of my pages, i print this form twice (different blocks), the form gets the same "form_id", so when i submit one of them and get an error, both of them get the error highlighted, and the fields populated.
I want that only the form i submit gets the errors, is there a way to do this?
Thanks!!
For anyone interested, to do this you need to use the hook_forms.
This hook only gets called when the form_id passed to a drupal_get_form doesn't exist, this is important, if you want to use this, make sure your calls use a non existing form_id, for example:
//Defining the form:
function mx_wtransnet_form_contacto($form, &$form_state, $block = null, $formType = null) {
}
I want to use this form multiple times and get different error handlers, instead of loading my form (mx_transnet_form_contacto), i'll call a non existing one:
$form = drupal_get_form("mx_wtransnet_form_contacto_invalid", "contacto-mini");
Then i create my hook:
function mx_wtransnet_forms($form_id, $args) {
$forms = array();
if (strpos($form_id, '_contacto_') !==false) {
$forms[$form_id] = array(
'callback' => 'mx_wtransnet_form_contacto',
);
}
return $forms;
}
This function will catch all my druapl_get_form calls that don't exist, so i can process/direct them, in my example, what i do is simply check that the form_id contains contacto and then set the callback for this form to the original function.
In this case better to create another form with different "form_id" but with the same submit handler.
Another case: when you output same form twice on the page it also may get JS errors because ID of form elements are the same.
In case you don't repeat the form code and its submit handler(DRY principle), I would recommend create a custom function that has the form array
function form_my_custom($form_id){
$form['my_first_field'] = array();
$form['my_second_field'] = array();
$form['#attributes']['id'] = $form_id;
$form['my_submit_button'] = array(
'#submit' => array('my_custom_form_submit')
);
return $form;
}
function my_block1_form(){
return my_custom_form('my_form_id_1');
}
function my_block2_form(){
return my_custom_form('my_form_id_2');
}
function my_custom_form_submit(&$form, &$form_state){
// your submit handler.
}