2

I have what I'm calling a "student blog" section. Students can create a blog either locally (with a Body field), or remotely using a Link. A single conditional_fields value of "blog is hosted" controls what is shown on the add_node_form.

These blog entries are moderated via Workbench Moderation.

Anyways in Views 3.x I want to make a listing of the blog titles, linked to their content. If the content was "local" I want a link to node/NID or the url alias. If the content was "remote url" I want the url to point to the offsite url for the blog post (eg, Blogger.com).

I have created the content type, and a simple views block that lists the node titles .... but I can't get the link(s) to point to the correct locations per entry.

If possible, please submit answers using Views 3.x Core only (not views_php, etc) ... this site has a ton of modules already :P

Images of configuration:

Content Type Fields enter image description here

Initial Blog Creation Screen enter image description here

Blog was Local screen enter image description here

Blog was Remote Url screen enter image description here

Views Block Output of Node Titles (that should link either local or remote enter image description here

2 Answers 2

3
+100

You could add a blog_url field which you hide in a form alter and programmatically set when the form is submitted. If the blog is remote, you set it to the value of the link field. If it's local, you set it to the value of the node. You can then use this field in Views (just include the Title field and use 'Link this field' to link it to the value of blog_url). Something along the lines of:

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'school_blog_node_form') {
    $form['field_blog_url']['#access'] = FALSE;
    $form['#validate'][] = 'mymodule_validate_school_blog_form';
  }
}

/**
 * Validate handler for school_blog edit form.
 */
function mymodule_validate_school_blog_form($form, &$form_state) {
  if ($form_state['values']['field_blog_is_hosted'] == 'local') {
    $form_state['values']['field_blog_url'] = 'node/' . $form['nid'];
  }
  else {
    $form_state['values']['field_blog_url'] = $form_state['values']['field_blog_remote_url'];
  }
}
1
  • Thanks, I am testing this approach now. I'll likely use it. I've done this exact approach in the past and basically had "programmers block" last week ahah. thanks again
    – tenken
    Commented Aug 20, 2012 at 15:35
1

This is the closest I can come up. This will render each row of the view -- and I alter it's output per row.

I could stick this code into a custom block from a module. Oober hacky given I know i'm only showing 1 link per entry.

$view = views_get_view('student_blogs');
$view->set_display('block');
$view->set_arguments(array('singapore'));
$view->execute();

$renderer = $view->style_plugin->row_plugin;
foreach ($view->result as $index => $row) {
  $view->row_index = $index;
  $is_remote_blog = ( $row->_field_data['nid']['entity']->field_blog_is_hosted[LANGUAGE_NONE][0]['value'] == 'remote site') ? 0 : 1;
  $remote_url = $row->_field_data['nid']['entity']->field_blog_remote_url[LANGUAGE_NONE][0]['url'];
  $output =  $renderer->render($row);
  if ($is_remote_blog) {
    $output = preg_replace('/(.*<a href=")(.*)(">.*)/i',"$1$remote_url$3", $output);
  }
  echo $output;
}
// Cant use views->preview as we alter what comes back from render().
//$view->preview();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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