0

I realize my request is a bit complex, I hope someone can answer.

I have a drupal 7 clean installation. I have a node type called Whole and another one called Piece. A Whole contains one to many Piece: I used the Entity module (with Entity reference) to link entities. The Piece node has a boolean field called selected.

When a user views a Whole node he can see all the Piece elements linked to it.

I need to allow an unlogged user to set the selected field of the piece node to true without leaving the Whole node page. I also need to keep track of the selected Piece elements (maybe in a session variable?). In the end I need to list all the Pieces altered in the session to compose a mail. When the mail is sent the session is cleared.

Is there a module that allows in place edit of node attributes?? I have installed and tried Editable Views, but it doesn't work. Maybe there is a conflict with my drupal version (7.14) which unfortunately I can't change.

2

You don't need to let anon visitors edit nodes to achieve this, and I wouldn't recommend it. It presents a security issue, and would store the "selected" status as a property of the node (for all visitors).

Rather, you can store the selected nodes in a session or a cookie, using a standalone drupal form. Create a form, like so:

function YOURMODULE_node_store_form($form, &$form_state, $nid) {
  $form_state['nid'] = $nid;
  $dv = (isset($_SESSION['node_store'] && in_array($nid, $_SESSION['node_store']) ? 1 : NULL;
  $form['selected'] = array(
    '#type' => 'checkbox',
    '#title' => 'Include this page in email.',
    '#default_value' => $dv,
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}

function YOURMODULE_node_store_form_submit($form, $form_state) {
  if($form_state['values']['selected'] && $form_state['nid']) {
    $_SESSION['node_store'][] = $form_state['nid'];
    drupal_set_message('The page will be included in the email.');
  }
}

In the template file for the Piece content type, add the form to the teaser display, like so:

<?php print render(drupal_get_form(MYMODULE_node_store_form, $node->nid);) ?>

To include the stored nodes in the mail, you can retrieve them from the $_SESSION['node_store'] array. Once the email has been sent, you should probably clear the $_SESSION['node_store'] array.

If you DO have to store it to the node, use the node_save function in your submit handler, instead:

function YOURMODULE_node_store_form_submit($form, $form_state) {
  if($form_state['values']['selected'] && $node = node_load($form_state['nid'])) {
    $node->field_selected[$node->language][0]['value'] = 1;
    node_save($node);
    $_SESSION['node_store'][] = $node->nid;
    drupal_set_message('The page will be included in the email.');
  }
}
  • I'll try your suggestion for sure, but I need users to edit permanently the field's value. Maybe it is safer to create a single account for all users to avoid unallowed accesses. – Gabber Jul 1 '14 at 14:33
  • ok, then I'm not sure I understand. Why do they need to permanently edit it? – Ollie Jul 1 '14 at 14:38
  • This should work like a sort of ecommerce chart, when a person gets a piece it's sold and can't be "bought" by another person. – Gabber Jul 1 '14 at 14:43
  • ok, I get it. :) I've updated the answer. – Ollie Jul 1 '14 at 14:50
  • Tested, two small mistakes in code but it's ok (+1) I still need to figure out how to make it work in a view, the form is created correctly but it returns an error on submission. Many thanks meanwhile :) – Gabber Jul 1 '14 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.