Download & Extend

New field for node published status

Project:Views
Version:5.x-1.6
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed (won't fix)

Issue Summary

This is for a site in which essentially all content is presented via views and formatted via views templates. Administrators should be able to preview content using these templates, without it being presented to unprivileged users. By removing the 'Node: Published' filter and checking for this field instead, data can be previewed before being shown to the public.

Small patch is attached. Since this value is never actually displayed to the end user, I didn't bother to make this field display 'Yes' or 'No'. Instead it just shows the actual contents of node.status (i.e., 0 or 1).

AttachmentSize
views_published_field.patch628 bytes

Comments

#1

In case anyone else is interested in using this, I should mention that the following code in the view template checks whether the current user has a given role (in my case, 'administrator'). This together with the $status field can be used to control which content is displayed.

<?php
$is_admin
= false;
global
$user;
if (
in_array('administrator', $user->roles)) {
   
$is_admin = true;
}
?>

#2

Status:needs review» needs work

I tested this against an installed copy of 5.x-1.6 -- seems to work fine, just what I needed.

I do think it would be better to make this display "Yes/No", rather than 0/1. The field value may not be visible in *your* application, but in general when this field is added to a view it will be displayed. Best to avoid confusing digits in favor of recognizable words.

I'm not in love with the fact that the field is called "Published" on the visible UI and "status" in the DB and in the API. But this patch is consistent with the rest of Drupal in that usage, and I think that's fine.

#3

This patch has a chance to be included in 1.7, if you account for the mentioned changes.

#4

Did this ever get advanced or included? I think all it would need would be a new function to handle converting 1/0 to Yes/No.

<?php
/**
* Convert the 1/0 value to Yes or No, respectively.
*/
function views_handler_field_yesno($fieldinfo, $fielddata, $value, $data) {
  switch(
check_plain($value)) {
    case
0: return t('No');
    case
1: return t('Yes');
  }
}
?>

... and then a line in the node_views_tables() saying "'handler' => 'views_handler_field_yesno',".

For what it's worth, I'm in favour of the "Node: Published" label since it is consistent with the already available filter for this data.

Find attached a patch (generated by comparing old and changed files using Komodo Edit - I don't have a patch roller but apparently it's the same format). While I was in there I also changed 'sortable' to true, since I don't see any reason to prevent people from sorting on this field. This seems to work under Views 5.x-1.6 (which is apparently still the recommended release for 5.x).

AttachmentSize
views_node_patch.txt 1.22 KB

#5

PS: I'm not exactly sure how this will behave given a non-1/0 input - I figured it was safer to do nothing than set a default, but I don't know how Views would react to a handler not returning. But that's the beauty of developing with preconditions!

#6

Status:needs work» reviewed & tested by the community

This patch works for me.

#7

Status:reviewed & tested by the community» closed (won't fix)

At this time, only security fixes will be made to the 5.x version of Views.

nobody click here