5

I am working on flex slider module and I want to add one button in caption so for that I have added one field in content type with Machine name: field_image_url and Field Type: text.

Now I am changing in core file of the module to fetch that URL into the front end.For that code is below:

    <?php
    /**
     * @file
     * Default output for a Flex Slider node.
    */
    ?>
    <div class="flex-nav-container">
      <div class="flexslider-content flexslider clearfix" id="flexslider-<?php print $id; ?>">
        <ul class="slides">
        <?php foreach($items as $item) : ?>
          <li><?php print render($item); ?>
            <?php if(!empty($item['#item']['title']) || !empty($item['#item']['alt'])) : ?>
             <div class="flex-caption">
             <h3><?php print $item['#item']['alt'];?></h3>
             <p><?php print $item['#item']['title']; ?></p>
             <?php if(isset($node->field_image_url)): ?>
                <a href="<?php print render ($node->$field_image_url); ?>" class="btn btn-theme">Learn More</a>
             <?php endif; ?>
             </div>
            <?php endif; ?>
          </li>
        <?php endforeach; ?>
        </ul>
      </div>
    </div>

I know I am making some mistake as I am very new to drupal development.

     <?php if(isset($node->field_image_url)): ?>
        <a href="<?php print render ($node->$field_image_url); ?>" class="btn btn-theme">Learn More</a>
     <?php endif; ?>

Above code is for fetching URL.It is not giving me any error, but it is also not showing me the button.I have already entered URL for one slide.

Where am I lacking ?

Edited :

I want to also mention that Slider is on front page and there is an article content just below to the slider.

I have given node path to the front page from the Configuration to show it on front page.

So now, If I do

     <?php 
        if (arg(0) == 'node' && is_numeric(arg(1))) {
          // Get the nid
          $nid = arg(1);
          // Load the node if you need to
          $node = node_load($nid); print_r($node);
        }
     ?>

It is giving me the node ID of the article.I want the node ID of the slider Image so that I can pull out the image URL field that I have added to the Slider Image Content type.

EDITED:

    <?php
        //this code will load all nodes from your content type and then print them
        $node_type = "slider_image"; 
        $result = db_query("SELECT nid FROM node WHERE type = '%s' ", $node_type);
        $nodes = array();
        while ($obj = db_fetch_object ($result)) {
          $node = node_load($obj->nid);
          echo $node->field_image_url['und'][0]['value'];
          echo '<br>';
          $nodes[] = node_load($obj->nid);
        }
    ?>

I have tried above code but it is giving me an error : Recoverable fatal error: Argument 2 passed to db_query() must be of the type array, string given, called in path to modules\modules\flexslider\theme\flexslider-container.tpl.php on line 19 and defined in db_query() (line 2345 of path to site\includes\database\database.inc).

So to resolve above error, I tried following code with help of this:

$result = db_query("SELECT nid FROM node WHERE type = ':type' ", array(':type' => $node_type));

But above code also gives me error : Fatal error: Call to undefined function db_fetch_object() in path to module\modules\flexslider\theme\flexslider-container.tpl.php on line 21

3 Answers 3

1

In general to get the value of field if the entity type is a node

$node = node_load($nid);
echo $node->field_image_url['und'][0]['value'];

or try to print the $node to figure out how to get it

echo '<pre>'; var_dump($node); var_dump($node->field_image_url); die;

in your situation try the next to figure out what is happen there

<?php
$node_type = "slider_image";
$sql = db_query("SELECT nid FROM node WHERE type = $node_type");
$results = db_query($sql);
foreach($results as $row){
   $node = node_load($row->nid);
   echo $node->field_image_url['und'][0]['value'];
   echo '<br>';  
}
?>
10
  • Thanks for your time.echo $node->field_image_url['und'][0]['value']; <-- this code give me an error and echo '<pre>'; var_dump($node); var_dump($node->field_image_url); die; <-- this code gives me article's node ID which is just below to the slider. Also want to mention that above given code in question is in to the module file not in to the theme file.
    – Twix
    Commented Jan 2, 2015 at 9:28
  • Actually I am not familiar about your module (flex slider module ) but I was talking in general, so you should be sure you load the correct node, and be sure this node type has this field, and then my code will work Commented Jan 2, 2015 at 9:39
  • In general, it works nice coz I have used same code in template.php for another field that I have added to the Article content type.I dont know the reason,but it only gets node ID of the article which is below the slider.Do you know anything that can give all the node ID of the current page ?
    – Twix
    Commented Jan 2, 2015 at 10:03
  • which content type you use to add the "field_image_url" field ?? Commented Jan 2, 2015 at 10:19
  • I have made new content type Slider Image and field_image_urlis having TEXT as ``field type`.
    – Twix
    Commented Jan 2, 2015 at 10:24
1

I would suggest using field_get_items or if you are using wonderful entity api module you can use entity_metadata_wrapper. Using ['und'][0]['value'] is not a best way to get field values in drupal.

Thank you.

-1

You can use KPR function that comes with devel module. Once you enable the module you could add kpr($fields) function and it show you the structure of data returned by drupal. In general it should be something like $fields->field_image_url['und'][0]['value'];

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.