Rectangle 27 2

Create a view of "files" and then under the Advanced collapsed fieldset on the right side of the Views UI, create a relationship (a join) of the node with the file.

Here's a quick view you can copy here and import into your views ui:

<?php
    $view = new view;
    $view->name = 'test';
    $view->description = '';
    $view->tag = 'default';
    $view->base_table = 'file_managed';
    $view->human_name = 'test';
    $view->core = 7;
    $view->api_version = '3.0';
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

    /* Display: Master */
    $handler = $view->new_display('default', 'Master', 'default');
    $handler->display->display_options['title'] = 'test';
    $handler->display->display_options['access']['type'] = 'perm';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['query']['options']['query_comment'] = FALSE;
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'full';
    $handler->display->display_options['pager']['options']['items_per_page'] = '10';
    $handler->display->display_options['style_plugin'] = 'default';
    $handler->display->display_options['row_plugin'] = 'fields';
    /* Relationship: File Usage: Content */
    $handler->display->display_options['relationships']['file_to_node']['id'] = 'file_to_node';
    $handler->display->display_options['relationships']['file_to_node']['table'] = 'file_usage';
    $handler->display->display_options['relationships']['file_to_node']['field'] = 'file_to_node';
    $handler->display->display_options['relationships']['file_to_node']['required'] = 0;
    /* Field: File: Name */
    $handler->display->display_options['fields']['filename']['id'] = 'filename';
    $handler->display->display_options['fields']['filename']['table'] = 'file_managed';
    $handler->display->display_options['fields']['filename']['field'] = 'filename';
    $handler->display->display_options['fields']['filename']['label'] = '';
    $handler->display->display_options['fields']['filename']['alter']['alter_text'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['make_link'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['absolute'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['word_boundary'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['ellipsis'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['strip_tags'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['trim'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['html'] = 0;
    $handler->display->display_options['fields']['filename']['hide_empty'] = 0;
    $handler->display->display_options['fields']['filename']['empty_zero'] = 0;
    $handler->display->display_options['fields']['filename']['link_to_file'] = 1;
    /* Field: Content: Body */
    $handler->display->display_options['fields']['body']['id'] = 'body';
    $handler->display->display_options['fields']['body']['table'] = 'field_data_body';
    $handler->display->display_options['fields']['body']['field'] = 'body';
    $handler->display->display_options['fields']['body']['relationship'] = 'file_to_node';
    /* Field: Content: Date */
    $handler->display->display_options['fields']['field_date']['id'] = 'field_date';
    $handler->display->display_options['fields']['field_date']['table'] = 'field_data_field_date';
    $handler->display->display_options['fields']['field_date']['field'] = 'field_date';
    $handler->display->display_options['fields']['field_date']['relationship'] = 'file_to_node';
    $handler->display->display_options['fields']['field_date']['alter']['alter_text'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['make_link'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['absolute'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['external'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['replace_spaces'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['trim_whitespace'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['nl2br'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['word_boundary'] = 1;
    $handler->display->display_options['fields']['field_date']['alter']['ellipsis'] = 1;
    $handler->display->display_options['fields']['field_date']['alter']['strip_tags'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['trim'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['html'] = 0;
    $handler->display->display_options['fields']['field_date']['element_label_colon'] = 1;
    $handler->display->display_options['fields']['field_date']['element_default_classes'] = 1;
    $handler->display->display_options['fields']['field_date']['hide_empty'] = 0;
    $handler->display->display_options['fields']['field_date']['empty_zero'] = 0;
    $handler->display->display_options['fields']['field_date']['hide_alter_empty'] = 1;
    $handler->display->display_options['fields']['field_date']['settings'] = array(
      'format_type' => 'long',
      'fromto' => 'both',
      'multiple_number' => '',
      'multiple_from' => '',
      'multiple_to' => '',
      'show_repeat_rule' => 'show',
    );
    $handler->display->display_options['fields']['field_date']['group_rows'] = 1;
    $handler->display->display_options['fields']['field_date']['delta_offset'] = '0';
    $handler->display->display_options['fields']['field_date']['delta_reversed'] = 0;
    $handler->display->display_options['fields']['field_date']['delta_first_last'] = 0;
    $handler->display->display_options['fields']['field_date']['field_api_classes'] = 0;
    /* Field: Content: Link */
    $handler->display->display_options['fields']['view_node']['id'] = 'view_node';
    $handler->display->display_options['fields']['view_node']['table'] = 'views_entity_node';
    $handler->display->display_options['fields']['view_node']['field'] = 'view_node';
    /* Field: Content: Title */
    $handler->display->display_options['fields']['title']['id'] = 'title';
    $handler->display->display_options['fields']['title']['table'] = 'node';
    $handler->display->display_options['fields']['title']['field'] = 'title';
    /* Sort criterion: File: Upload date */
    $handler->display->display_options['sorts']['timestamp']['id'] = 'timestamp';
    $handler->display->display_options['sorts']['timestamp']['table'] = 'file_managed';
    $handler->display->display_options['sorts']['timestamp']['field'] = 'timestamp';
    $handler->display->display_options['sorts']['timestamp']['order'] = 'DESC';

    /* Display: Page */
    $handler = $view->new_display('page', 'Page', 'page');
    $handler->display->display_options['path'] = 'test';

?>

As for the correct markup surrounding the entire RSS printout, you can manipulate the views templates. To modify each record, use views-view-fields--test.tpl.php (for example). And for the wrapper around the entire view, use views-view--test.tpl.php (for example). The templates available are under the "Themes" link at the bottom of the third rail (the collapsed one) on the Views UI. Just drop the views template files into your theme's folder and drush cc all.

Thank you, that got me on track. Over the weekend I started writing a views_mrss module based on views_rss implementing "MRSS - Fields" and that's made it so I don't have to use any views templates. I'll publish it once it's up to par.

Although a File view is correct in some instances, and does nearly address my question as posed, it actually fails in one important respect: Although you can add a "File's Content" relationship to get the Content associated with the file, you can't then use the "Content: Audio" or (in my actual case, "Content: Video") field because that returns all the Audio/Video values associated with the Content. And unfortunately the "File: fid" element is too general to derive the Video's thumbnail. But a Content view with a Fields display provides complete media elements that I can utilize.

in the case that you'd want to filter based upon the join's content type: If the views filter "Content: Type" isn't available in the views UI, look at hook_views_query_alter() and see if you can alter the join portion of the mysql query. That's getting fairly advanced in mysql statements. Someone chime in? --Joe

Drupal 7: Make an RSS feed with multiple s per node? - Stack Ove...

drupal rss drupal-7 drupal-views mediarss
Rectangle 27 2

Import or aggregate data as nodes, users, taxonomy terms or simple database records.

Drupal 7: Import CSV to multiple taxonomy fields in node - Stack Overf...

drupal drupal-7 drupal-modules
Rectangle 27 3

I think the reason it's not working is because the node element of the webform tokens refers to the node of the webform itself, not the current node page. Why webform doesn't use the in-built token system I have no idea, seems like a missed opportunity.

Looking at the webform module I think the easiest way to get this done is to write a quick custom module defining a single table (two columns, nid and url) and then implement hook_webform_submission_insert() to save the current $_SERVER['REQUEST_URI'] for all webforms. You can then use hook_webform_submission_load() and hook_webform_submission_render_alter() respectively to re-attach the URL data to the submission and display it to the page.

You can find all of those hooks in the webform_hooks.php file included in the module, they're pretty well documented.

Interesting. I haven't actually made any custom modules, but I think I'll definitely look into this. Do you know if there's a way to get the "current node" from the page you're seeing? (so instead of getting $_SERVER['REQUEST_URI'] I actually get the node's title or whatever)

another idea I had is to just use some quick and dirty jQuery and load $('h1').text() into the damn hidden field... but it seems dirty and won't work if JS is disabled

Yeah that's not ideal, another idea might be to use a hook_form_alter() to populate the hidden field, that way it'll work for non-JS users. You can use menu_get_object() if you're on a node page to get the node object

function funnygarbage_form_webform_client_form_22_alter(&$form, &$form_state)     {     	$current_object = menu_get_object();     	if ($current_object->type != 'job')     		return;     	$job_title = $current_object->title;     	     	$form['submitted']['job_position']['#default_value'] = $job_title;     }

Drupal 7 Webform fills hidden field with current node title - Stack Ov...

drupal drupal-7 drupal-webform
Rectangle 27 2

Sure you can, just enable the php filter module, and put the php code to do this in the body of the node. Make sure you set the format of the body to PHP and not plain text or html.

So if the image is in your theme folder, you can grab and display display it using:

<img src="<?php print path_to_theme() . "/files/image.jpg"; ?>" title='some image'/>

You can get all files within a directory using php's scandir function. This function will return an array of all files within your files directory, after which you can loop through this array and output all image files to the screen like:

$files = scandir("path/to/files/dir");
/*Unset the first 2 items in the array since they contain . and .. respectively */
unset($files[0]);
unset($files[1]);
foreach($files as $file)
{
    /* 
      Here we get the file extension 
      If the value of $file = "photo.jpeg"; this returns "jpeg"
    */
    $f_ext =  end(explode(., $file));   
    /*Checking if file is an image*/
    if($f_ext == 'jpg' || $f_ext == 'png' || $f_ext == 'gif'|| $f_ext == 'jpeg')
    {
         print "<img src='<?php print "path/to/files/$file"; ?>' title='some image'/>";
    }
}

Hello, Joshua. I have gotten that far. If I have, for example, images that I have upload to my 'files' folder of my theme and I want to display all of them on one of my node pages, what is the PHP code I would use. Again, I am using Drupal 7 core. Thanks.

Thanks, Joshua. Is there a particular way to do this as an array. I want to try it displaying ALL the images contained in the files folder. I really appreciate your help.

Hi, Joshua. Sorry I keep bugging you with this. I can't get it to work properly. I tried the above code with no success. I'll list what I did to customize the code you supplied and give you the error that is showing. 1) I changed the path/to/files to the image path that I have tested and works, in both the variable above and in the print path . 2) I wrapped the code with <?php ?>. 3) I didnt change the if statement parameter at all, do I need to? this error contnues to show: Parse error: syntax error, unexpected T_STRING in theme-site\modules\php\php.module(80) : eval()'d code on line 8

Hey man, i don't mean to be disrespectful or anything. But if you can't understand the code above that i have updated, i would advise you to read a php book, preferable one that would take you from basic to intermediate. :) all the best

Drupal 7. Adding images to a node with PHP - Stack Overflow

php image drupal drupal-7
Rectangle 27 30

$filename = 'image.txt';
$image = file_get_contents('http://www.ibiblio.org/wm/paint/auth/gogh/gogh.white-house.jpg');
$file = file_save_data($image, 'public://' . $filename, FILE_EXISTS_RENAME);
$node->field_image = array(LANGUAGE_NONE => array('0' => (array)$file));

Attaching image files to nodes programmatically in Drupal 7 - Stack Ov...

drupal drupal-7
Rectangle 27 30

$filename = 'image.txt';
$image = file_get_contents('http://www.ibiblio.org/wm/paint/auth/gogh/gogh.white-house.jpg');
$file = file_save_data($image, 'public://' . $filename, FILE_EXISTS_RENAME);
$node->field_image = array(LANGUAGE_NONE => array('0' => (array)$file));

Attaching image files to nodes programmatically in Drupal 7 - Stack Ov...

drupal drupal-7
Rectangle 27 17

Node are entities, so you can use the EntityFieldQuery class to query for them. This way, you don't have to bother how the information is stored in the database. And, shall you need to add additional filters based on field values, code evolution will be easy.

$result = (new EntityFieldQuery())
  ->entityCondition('entity_type', 'node')
  ->propertyCondition('title', $title)
  ->execute();
$title_is_unique = empty($result['node']);

That's an awful lot of overhead for such a simple query, field storage doesn't extend to entity properties so using EntityFieldQuery here has no real benefit

Yep, there is some overhead using EntityFieldQuery to query a single node property. But since it will only apply to a multi-nodes creation process, there is a good chance that the performance cost will be marginal. And it will be easier to maintain should the node creation process evolve into something that should check for several conditions.

If you use Entity translation and the Title module, the code needs to be updated to query for the title_field field instead of the title property.

I know this is many years later, but there's a bug in this code - you can't chain a PHP constructor. In 5.4 and up you can use (new EntityFieldQuery())->, but as written this should give a fatal error

Fixed with added ().

Programmatically getting nodes by title in Drupal 7 - Stack Overflow

drupal drupal-7 drupal-modules
Rectangle 27 32

module_load_include('inc', 'node', 'node.pages');

$form = node_add('nodetype');
$output = drupal_render($form);

If your node form has a file upload widget, you should add the following lines to the menu array:

'file path' => drupal_get_path('module', 'node'),
'file' => 'node.pages.inc',

Can you add an example on how to "add following lines to the menu array"?

about file upload widget

@MariusIlie I am new to drupal so were do I put this code. I have tried it in hook_node_view in this hook I have add the $output like this $node->content['article'] = $output; but nothing happen. So after render how to attach it to the node view?

Drupal 7 - How to display node/add/sometype form on another page? - St...

drupal drupal-7
Rectangle 27 18

To get a node edit form, you need to include node.pages.inc.

<?php
  // required for Drupal 6
  module_load_include('inc', 'node', 'node.pages'); 
  // which nodeform you want
  $node_type = 'YOURNODETYPE';
  $form_id = $node_type . '_node_form';
  // maybe add current users info
  global $user;
  // create a blank node
  $node = array(
    'uid' => $user->uid,
    'name' => (isset($user->name) ? $user->name : ''),
    'type' => $node_type,
  );
  // Invoke hook_nodapi and hook_node
  node_object_prepare($node);
  // Or you can also use an exiting node, for example
  // $node = node_load(123);
  // and the display the form:
  $output = drupal_get_form($form_id, $node);
?>

"create a blank node" section isn't working for drupal 7, but I found a solution for that. thanks

The code above is wrong for d7. As said @user462645 below, the $node variable has to be an object module_load_include('inc', 'node', 'node.pages'); $node_type = 'yournodetype'; $form_id = $node_type . '_node_form'; global $user; $node = new stdClass(); $node->uid = $user->uid; $node->name = (isset($user->name) ? $user->name : ''); $node->type = $node_type; $node->language = ''; node_object_prepare($node); return drupal_get_form($form_id, $node);

this method return me error in drupal 7,but Marius Ilie solution work correctly.

Drupal 7 - How to display node/add/sometype form on another page? - St...

drupal drupal-7
Rectangle 27 8

You could get all the node titles with a SQL query like:

$row = db_query('SELECT nid FROM {node} WHERE title = :mytitle', array(':mytitle' => $myNewTitle))->fetchField();
 if(!isset($row['nid'])) {
    //safe to continue
 } else {
    //that title is already there and its in node with node id "nid"
 }

And if there are no results returned then you may assume you're good to go.

Or alternatively you could edit the node table to make the title field UNIQUE and handle the error thrown by MySQL. Don't really know how feasible that is though.

+1 this is the best way to accomplish this. You should consider joining the revisions table in though to get the latest version of the data

Wow, thanks a lot. I'll take this approach :)

Programmatically getting nodes by title in Drupal 7 - Stack Overflow

drupal drupal-7 drupal-modules
Rectangle 27 7

If your field has multiple cardinality you can add extra items like this:

$node->field_sup_id[LANGUAGE_NONE] = array(
  0 => array('value' => $the_id),
  1 => array('value' => $other_id)
);

And you can use the language element of the array to define what language this particular field value pertains to:

$lang = $node->language; // Or 'en', 'fr', etc.
$node->field_sup_id[$lang] = array(
  0 => array('value' => $the_id),
  1 => array('value' => $other_id)
);

Add these before your call to node_save() and the field contents will be added/updated when you do call it.

php - How to set custom field value for node in Drupal 7? - Stack Over...

php drupal drupal-7
Rectangle 27 2

function theme_preprocess_page(&$vars) {
 if (isset($vars['node']->type)) {
  switch ($vars['node']->type) {
   case 'news':
   case 'blog':
   case 'event':
   case 'page':
    $vars['theme_hook_suggestion'] = 'page__alt';
    break;
  }
}

templates - Drupal 7: How to create one node page for multiple content...

templates preprocessor content-type
Rectangle 27 6

$file_temp = file_get_contents('public://someimage.jpg');

// Saves a file to the specified destination and creates a database entry.
$file_temp = file_save_data($file_temp, 'public://' . 'someimage.jpg', FILE_EXISTS_RENAME);

$node->field_page_image = array(
  'und' => array(
    0 => array(
      'fid' => $file_temp->fid,
      'filename' => $file_temp->filename,
      'filemime' => $file_temp->filemime,
      'uid' => 1,
      'uri' => $file_temp->uri,
      'status' => 1,
      'display' => 1
    )
  )
);

Drupal community documentation gives slightly different way for doing this. drupal.org/node/2301425 . It says file_usage_add($file, 'file', 'node', $node->nid); must also be done while doing this.

You may also want to unlink the temporary file manually, if that is no longer needed. Otherwise it will accumulate to become a huge waste of storage space over time.

There is no temporary file in this case. The image is being downloaded to memory only, then stored in its target destination.

Attaching image files to nodes programmatically in Drupal 7 - Stack Ov...

drupal drupal-7
Rectangle 27 6

$file_temp = file_get_contents('public://someimage.jpg');

// Saves a file to the specified destination and creates a database entry.
$file_temp = file_save_data($file_temp, 'public://' . 'someimage.jpg', FILE_EXISTS_RENAME);

$node->field_page_image = array(
  'und' => array(
    0 => array(
      'fid' => $file_temp->fid,
      'filename' => $file_temp->filename,
      'filemime' => $file_temp->filemime,
      'uid' => 1,
      'uri' => $file_temp->uri,
      'status' => 1,
      'display' => 1
    )
  )
);

Drupal community documentation gives slightly different way for doing this. drupal.org/node/2301425 . It says file_usage_add($file, 'file', 'node', $node->nid); must also be done while doing this.

You may also want to unlink the temporary file manually, if that is no longer needed. Otherwise it will accumulate to become a huge waste of storage space over time.

There is no temporary file in this case. The image is being downloaded to memory only, then stored in its target destination.

Attaching image files to nodes programmatically in Drupal 7 - Stack Ov...

drupal drupal-7
Rectangle 27 5

// Drupal 7    
// Embed node creation form on a custom page inside module.
module_load_include('inc', 'node', 'node.pages');
$form = node_add('node_machine_name'); 
return drupal_render($form);

Drupal 7 - How to display node/add/sometype form on another page? - St...

drupal drupal-7
Rectangle 27 5

  • Make sure you've also copied across the core node.tpl.php file to your theme folder; the template overrides for nodes won't work without it.
  • Make sure your file is properly named, e.g. for a content type with the name of home_page the template file will be called node--home_page.tpl.php.
  • Make sure you've cleared Drupal's caches since adding the new template files; the theme registry is cached so a clear is necessary to pick up changes.

As an aside, the node template file doesn't have any blocks/regions in it, they will always be contained in page.tpl.php, or some derivative.

php - Drupal 7 custom template file for certain content type - Stack O...

php templates drupal drupal-7
Rectangle 27 4

You can use arg(1) in a piece of custom code to get the current node id (but only if it is a node you're on). See http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/arg/7. For instance you could easily embed your view in a template of your choice or a within a theme function like this:

views_embed_view('name_of_your_view', 'display_name', arg(1));

To just place a link on that timeperiod node to that page (generated by your view) you would probably use a preprocess function in conjunction with a template (print if set). The possibilities there are uncountable, so you might need to provide further informations, where to place the link. But basically it can be achieved by using `arg(1)'.

Drupal 7: Use current node id to dynamically generate link on page? - ...

drupal drupal-7 drupal-panels
Rectangle 27 3

Here's one extra bit that tripped me up for a while: this will attach the image to the node, and if you're adding the image then you're okay. However, if you're updating an image, and you care about displaying it on a page, then one extra step is needed before calling node_save():

image_path_flush($node->field_image['und'][0]['uri']);

This will regenerate all of that image's styles.

Attaching image files to nodes programmatically in Drupal 7 - Stack Ov...

drupal drupal-7
Rectangle 27 3

Here's one extra bit that tripped me up for a while: this will attach the image to the node, and if you're adding the image then you're okay. However, if you're updating an image, and you care about displaying it on a page, then one extra step is needed before calling node_save():

image_path_flush($node->field_image['und'][0]['uri']);

This will regenerate all of that image's styles.

Attaching image files to nodes programmatically in Drupal 7 - Stack Ov...

drupal drupal-7