1

I'm stuck in Drupal Panels / PHP Access plugins.

At least, now I found the three conditions to create my final snippet. the purpose of it is to return TRUE; if "condition1 is TRUE" OR "condition2 is TRUE" OR "condition3 is TRUE". I found a lot of similar questions, but the last condition force me to post here to find the right way to do this.

Condition 1:

// At least  $view1->result has result.
$view1 = views_get_view('sp_onglet_videos');
$view1->set_display('views-tab-embed_1');
$output1 = $view1->preview();
if ($view1->result) {
  return TRUE;
}

Condition 2 (same thing):

// At least  $view2->result has result.
$view2 = views_get_view('sp_onglet_audio');
$view2->set_display('views-tab-default');
$output2 = $view2->preview();
if ($view2->result) {
  return TRUE;
}

Condition 3 is more complex:

// Checks for content in the field field_txt_videos.
if (isset($contexts['argument_nid_1']->data-> field_txt_videos)) {
  $field = $contexts['argument_nid_1']->data-> field_txt_videos;
  if (is_null($field)) {
    return FALSE;
  }
  if (is_array($field)) {
    foreach ($field as $key => $val) {
      if (is_array($val)) {
        $field[$key] = array_filter($val);
      }
    }
    $field = array_filter($field);
    return count($field);
  }
  if (is_string($field) && trim($field) == '') {
    return FALSE;
  }
  if ($field) {
    return TRUE;
  }
  return FALSE;
}

I would like to have something clean (and functional) like this:

if ($view1->result && $view2->result && $field) {
return TRUE;
      }

But it's to tricky for my php knowledge. Need a little help !

2 Answers 2

0

You want to save the result of the 3rd condition (into a variable) and use this result to run your final condition/query. But you can query the 3rd condition if it is a function.

Sign up to request clarification or add additional context in comments.

2 Comments

huh, yes something like this, but i'm lost with all this "if", "return", etc. I'll try to find how to do this.
I corrected my answer. You should make a functiom out of the 3rd condition.
0

It is better to properly space your code and use plenty of newlines.

However, PHP does have some pretty cool tricks to do assignment inside conditional statements.

if(($view1 = views_get_view('sp_onglet_videos')) AND $view1->set_display('views-tab-embed_1') AND ($output1 = $view1->preview()) AND $view1->result) return TRUE;

However, as you can see this code is a mess - don't do it unless your assignment is really small. Take this simple security check at the top of a PHP file:

<?php defined('BASE_PATH') OR die('Not Allowed');

5 Comments

Thanks for your advice Xeoncross, but I found the first condition here drupal.org/node/446798 . you structure does not output the same result, I think. I try to understand: for me the variables are declared before the if condition.
I don't want to test a path or a constant. It's the result of views, or DB queries, if you don't know drupal.
@elektrorl I just wanted to show you that you can also set variables inside conditions and have their values tested if($value = hi()){...}.
ha ok I'm learning web development in the wrong way, first Drupal and when I'm really stuck after a lot of modules usage, I have to learn PHP, I'm so stupid. thanks for the advice, and do you think it is possible to chain the three conditions here?
@elektrorl, why don't you stop by videos.code2design.com and watch some of the PHP videos? There are also lots of other good PHP video sites out there that could save you some time if you can just spend a couple hours watching all them.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.