I have these field settings:

  • Required: No
  • Allowed views: selected one
  • nothing else ticked

When creating content I see am option in the viewfield. Selecting it and then clicking on preview lists the first view (of the site) instead of none.

I found this: viewfield.module

function _viewfield_potential_references($field_settings = TRUE, $field = array(), $delta = 0) {
...
  if (!$field_settings && (!$field['required'] || ($field['multiple'] && $delta > 0))) {
//     drupal_set_message("<pre>" . print_r($options, TRUE) . "</pre>");
//     array_unshift($options, '<'. t('none') .'>');
    $options[NULL] =  '<'. t('none') .'>';

Otherwise it gives a value of '0' later on.

function viewfield_field($op, &$node, $field, &$items, $teaser, $page) {
...
    case 'sanitize':
      
      foreach ($items as $delta => $item) {
        if (empty($item['vname'])) {
          $items[$delta] = array("vname"=>NULL,"vargs"=>NULL);
          continue;
        }
        $item = empty($item['vname']) || $field['widget']['force_default'] ? $field['super_default_widget'] : 

This fixes the problem for me. But I think it needs a better fix in case people use the force default stuff.

Great module BTW!!!

Comments

miiimooo’s picture

Actually I think I found a simple solution for this - although it's more like a workaround but might help someone else.

function _viewfield_potential_references($field_settings = TRUE, $field = array(), $delta = 0) {
..
  if (/*!$field_settings && */(!$field['required'] || ($field['multiple'] && $delta > 0))) {
    array_unshift($options, '<'. t('none') .'>');
  }

This allows setting the default view to in the field settings and leaves the default on when editing content.

karens’s picture

Priority: Normal » Critical
Status: Active » Needs review

If you don't choose the option to 'force default' the field still saves whatever view is selected as a 'global default value'. If you haven't selected a view, it ends up saving the first view in the list as the 'global default value'. Then on pages where you don't select any view there is some borked logic that displays the global default view even if you have not chosen 'force default'.

This fix doesn't take care of the borked logic, but at least allows the default to be no view instead of whatever view happens to be the first one in the list.

This is a fairly critical problem that actually makes the field unusable if you don't want a default view.

karens’s picture

To replicate the problem:

1) Add a viewfield to a content type.
2) Leave 'force default' unchecked and select no view as a default and make it not required.
3) Create a new page for this content type and leave the viewfield empty to display no view on that page.
4) You will see the first view in the viewfield list instead of no view.

bdimaggio’s picture

Thanks for this snippet miiimooo. This problem was kicking my ass, and would have taken me ages longer to figure out without that.

les lim’s picture

Marked #484850: Not choosing any view not possible as duplicate of this issue.

murokoma’s picture

Hi there,

can someone please post the full corrected viewfield.module code/file?

I am not quite sure where to put the additional 3 lines within that block, and I'm also having the issue that always *something* is displayed....

Thanks a lot!
Robert

raspberryman’s picture

Issue is reproducible with today's -dev.

willhowlett’s picture

Subscribing

askibinski’s picture

StatusFileSize
new775 bytes

solution mentioned in #1 works.

Here is a patch for it.

Any reason not to commit this to dev?

mediamash’s picture

critical for futher use indeed, needs update

jupiterchild’s picture

Installed viewfield and immediately hit this problem. Pretty much killed what i wanted to do until I came across this thread.

I manually applied the patch from #9 and can confirm this fixed the issue for me.

sdk’s picture

subscribing

pepe roni’s picture

@the maintainers:

Is there a reason why you didn't add this path to the current (march 27th) dev release?

I helped myself by creating an always empty view (node-id = 0) and set this view as default in the field settings of the viewfield. But I would better like the patch to be included in the next dev release, please!

sun’s picture

Status: Needs review » Needs work
+++ viewfield.module	2010-01-29 10:29:42.000000000 +0100
@@ -279,7 +279,10 @@ function _viewfield_potential_references
-  if (!$field_settings && (!$field['required'] || ($field['multiple'] && $delta > 0))) {
+  //if (!$field_settings && (!$field['required'] || ($field['multiple'] && $delta > 0))) {
+  //  array_unshift($options, '<'. t('none') .'>');
+  //}
+  if (/*!$field_settings && */(!$field['required'] || ($field['multiple'] && $delta > 0))) {

Why didn't you just remove !$field_settings and the parenthesis that wrap the directly following condition?

Powered by Dreditor.

sun’s picture

Status: Needs work » Needs review
StatusFileSize
new2.87 KB

Fresh to this module, but the proposed fix looks wrong - presuming that the calling code is right, of course.

Attached patch should be the proper fix, also clarifying the logic.

sun’s picture

Title: Selecting <None> displays the first view of the site (and it's not even in the allowed list ;) » Selecting <None> displays the first view of the site
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new4.67 KB

Alrighty, this time for real - the calling code was bogus, too.

askibinski’s picture

sun >
thanks, that works (and looks better ;)

willhowlett’s picture

Is this going to get commited soon do you think? I really need this functionality and would prefer to use an up to date version rather than use patches / workarounds.

willhowlett’s picture

Patch works great by the way, many thanks. Needed to uninstall the module and reinstall first (is that because the inc file makes changes to the database? I'm new to this)

sun’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for reporting, reviewing, and testing! Committed to all branches.

A new development snapshot will be available within the next 12 hours. This improvement will be available in the next official release.

roball’s picture

"Next official release" would be fine - there has not even been a release for 6.x yet. Any plans?

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.