Drupal StackExchange

Adding images to a node

1 month 3 weeks ago

Is there a way to programmatically add an array of images from a particular file path? I want to add all the images from one folder in my drupal site and add fancybox styling to them. Can this be done? I've tried looking on drupal.org with no success.

James30

Proxying a subdirectory to a remote site

1 month 3 weeks ago

The goal

Make all requests to a previously static page at https://www.example.com/sub/folder on server 1 (Proxy) ACTUALLY go to a Drupal site at https://other.example.com on server 2 (Origin)

This is also getting setup as a multisite, because there are sub/folder2, sub/folder3, etc. that will need to be converted to Drupal sites. I don't think this has anything to do with the problem, but for the sake of completeness, I'm adding the sites.php changes too below.

What I've done so far

Changes on Server 1 (Proxy): Apache config

RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} ^/sub/folder RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} SSLProxyEngine On <Location /sub/folder> ProxyPass https://other.example.com/ ProxyPassReverse https://other.example.com/ ProxyPassReverseCookiePath / /sub/folder </Location>

Changes on Server 2 (Origin): settings.php and sites.php

sites.php (because of multisite; and sites/sitename/settings.php is the file I'll be referencing below)

$sites['other.example.com'] = 'sitename'; $sites['www.example.com.sub.folder'] = 'sitename'; // I don't think this one is necessary, but just in case?

Aside from the standard DB setup in settings.php, the following has been added:

// $base_url allows for links/URIs to be generated properly // Changing the REQUEST_URI seems to have fixed form issues, like the search block/form // Not sure if the cookie_domain is doing anything/working properly if (isset($_SERVER['HTTP_X_FORWARDED_HOST']) && $_SERVER['HTTP_X_FORWARDED_HOST'] == 'www.example.com') { $base_url = 'https://www.example.com/sub/folder'; // NO trailing slash! $_SERVER['REQUEST_URI'] = '/sub/folder' . $_SERVER['REQUEST_URI']; // (Edit) note don't put 'http' in front of this $cookie_domain = '.other.example.com'; } ... $conf['reverse_proxy'] = TRUE; ... $conf['reverse_proxy_addresses'] = array('ip.of.origin.server');

The problems

  1. Trying to login through www.example.com/sub/folder/user validates (according to Drupal log)... but then it treats the page re-load as an anonymous user and fails access. Presumably, the cookie gets broke/lost/invalidated somehow in between. So, authenticated access to the server has to be done through other.example.com Not a huge problem (since the average Joe won't be logging in), but...
  2. There's just a lot of annoying things occuring having to deal with fixing user/content created links and URIs. Currently, links that are first viewed after a cache clear are cached as whatever domain they're being viewed from. (This happened from using Pathologic module to force absolute URLs.) I realize this might not have been the BEST solution--using Pathologic--but if I fix problem 1., this should become a moot point.

So, I think I've got some kind of SSL/Proxy cookie issue at this point... any suggestions?

UnsettlingTrend

Pager not working when $form['table'] is called with AJAX

1 month 3 weeks ago

For a statistics page I let users select the right file in a select list. After that I generate a table with stats. This table also has a pager (that worked without the ajax part).

Now I did everything in an ajax callback, when I now want to click on next page the URL is: http://localhost/drupal2/system/ajax?page=1 and that doesn't work ofcourse.

My code:

function push_notifications_theme() { return array( 'push_notifications_form_table' => array( 'render element' => 'form', ), ); } function push_notifications_form_table_form($form = array(), &$form_state) { $form = array(); // $id = $_GET['id']; $qry = db_select('push_notifications_api', 'pt'); $qry->distinct(); $qry->fields('pt', array('app_name')); $qry->orderBy('app_name', 'ASC'); $names = $qry->execute(); foreach($names as $record) { $options[$record->app_name] = $record->app_name; } $form['title'] = array( '#type' => 'fieldset', '#title' => t('Application statistics'), '#description' => t('Select an application below to see the statistics.'), '#tree' => TRUE, ); $form['title']['changethis'] = array( '#title' => t('Application Name'), '#type' => 'select', '#options' => $options, '#required' => TRUE, '#ajax' => array( 'callback' => 'push_notifications_form_table_form_callback', 'wrapper' => 'replace_textfield_div', ), ); $form['replace_textfield'] = array( '#prefix' => '<div id="replace_textfield_div">', '#suffix' => '</div>', ); if(!empty($form_state['values']['title']['changethis'])) { $appname = $form_state['values']['title']['changethis']; $id = db_query('SELECT app_id FROM push_notifications_api WHERE app_name = :app_name', array(':app_name' => $appname))->fetchField(); $form['replace_textfield']['id'] = array( '#type' => 'fieldset', '#title' => t('@appname: Push notifications', array('@appname' => $appname)), '#description' => t('Newest push notifications are on top.'), '#weight' => 10, ); $go_back_link = array( '!link' => l(t('go back to the overview'), 'http://localhost/drupal2/admin/config/services/push_notifications'), ); $form['replace_textfield']['goback'] = array( '#type' => 'fieldset', '#title' => t('Go back'), '#description' => t('If you want to view stats from other application you can !link', $go_back_link), '#weight' => 25, ); // $query = db_query('SELECT * FROM push_notifications_messages WHERE msg_appID = :app_id ORDER BY msg_timestamp DESC', array(':app_id' => $id)); // $qCount = db_query('SELECT * FROM push_notifications_messages WHERE msg_appID = :app_id', array(':app_id' => $id))->rowCount(); $query = db_select("push_notifications_messages", "n"); $query->fields("n", array('msg_id', 'msg_message', 'msg_receiver', 'msg_device', 'msg_appID', 'msg_timestamp')); $query->orderBy('msg_timestamp', 'DESC'); $query->condition('msg_appID', $id); $query = $query->extend('TableSort')->extend('PagerDefault')->limit(15); $result1 = $query->execute(); $form['replace_textfield']['table'] = array( '#theme' => 'table', '#header' => array(t('Message'), t('Device'), t('Token'), t('Date')), 'rows' => array(), '#weight' => 15, ); foreach($result1 as $key => $result) { $form['replace_textfield']['table']["#rows"]["'r$key'"] = array( 'c1' => array( 'data' => array('#type' => 'item', '#markup' => t('@message', array('@message' => $result->msg_message))), ), 'c2' => array( 'data' => array('#type' => 'item', '#markup' => t('@device', array('@device' => $result->msg_device))), ), 'c3' => array( 'data' => array('#type' => 'item', '#markup' => t('@token', array('@token' => substr($result->msg_receiver, -50)))), ), 'c4' => array( 'data' => array('#type' => 'item', '#markup' => t('@timestamp', array('@timestamp' => date("d F Y H:i:s", $result->msg_timestamp)))), ), ); } $form['replace_textfield']['pager'] = array('#markup' => theme('pager'), '#weight' => 20); } return $form; } function push_notifications_form_table_form_callback($form, $form_state) { // The form has already been submitted and updated. We can return the replaced // item as it is. return $form['replace_textfield']; } function theme_push_notifications_form_table(&$variables) { // Get the userful values. $form = $variables['form']; $rows = $form['rows']; $header = $form['#header']; // Setup the structure to be rendered and returned. $content = array( '#theme' => 'table', '#header' => $header, '#rows' => array(), ); // Traverse each row. @see element_chidren(). foreach (element_children($rows) as $row_index) { $row = array(); // Traverse each column in the row. @see element_children(). foreach (element_children($rows[$row_index]) as $col_index) { // Render the column form element. $row[] = drupal_render($rows[$row_index][$col_index]); } // Add the row to the table. $content['#rows'][] = $row; } // Redner the table and return. return drupal_render($content); }

Screenshot of result:

user3428971

Form API Field Permission

1 month 3 weeks ago

I am creating a custom form and storing data in custom table. I was wondering if there is any method to apply field level permission like

$form['value']['#permission'] = array('access this field');

I know I am having field permission module to apply permission on core fields but how I can achieve this?

Shabir A.

Form is not being rendered in template file

1 month 3 weeks ago

I have tried drupal_render,drupal_get_form etc for rendering the form,but the form is not being rendered. Is there any error in code or else what else should i add to the code so that it works.

function questionnaire_template_form($form, &$form_state, $question) { print_r("in template function"); $form_build = unserialize($question->criteria_data); foreach ($form_build as $key => $value) { $options = ($form_build[$key]['1']); $list = explode("\n", $options); $list = array_map('trim', $list); $list = array_filter($list, 'strlen'); foreach ($list as $key => $value) { $new = explode('|',$value); $new_key = reset($new); $new_value = end($new); $new_list = array($new_key => $new_value); dsm($new_list); } dsm('next'); $fb = explode("_",$key); $type =reset($fb); if($type == 'radio') { if($form_build[$key]['0']) { $form['radio_form'] = array( '#type' => 'radios', '#options' => $list, ); } } elseif($type == 'checkbox') { if($form_build[$key]['0']) { $form['checkbox_form'] = array( '#type' => 'checkboxes', '#options' => $list, ); } } elseif($type == 'selectlist') { if($form_build[$key]['0']) { $form['selectlist_form'] = array( '#type' => 'select', '#options' => $list, ); } } } return $form; }

Above code is my form.

<div> <?php //print_r($variables['entity']->qid);?></br><?php print_r($variables['entity']->title);?></br><?php print_r($variables['entity']->text);?></br><?php ?></br><?php ?></br><?php if($variables['entity']->criteria) { ?><br/><?php $form_template = drupal_get_form('questionnaire_template_form' , $variables['entity']); print(drupal_render($form_template)); } ?></br><?php print_r(l( t("Yes"), 'questionnaire/' . $variables['entity']->yes_quid));?></br><?php print_r(l( t("No"), 'questionnaire/' . $variables['entity']->no_quid));?></br><?php ?> </div>

And this is the template where I am rendering the form. I am getting the form if I dsm the form

neetu morwani

How to get replacement pattern for Search API's fulltext search in global text area header

1 month 3 weeks ago

In Drupal 7, I'm currently using the default core search, and a view for node search results. I have a global text to pull in the replacement pattern for the term that were searched for. I get the token by adding the contextual filter Search: Search Terms I am using the replacement pattern %1 as seen in the picture:

Then I put <span class="MY-CLASS">You searched for:</span> <b> %1 </b> in my global text to display the keywords that were searched for:

This all works great for Drupal core search, but I am not finding a way to do this with the view in Search API? I am using Database Service, and a view built from the database index I created. I have tried to find a field such as keywords for the full text search, but since the view is based off of indexed content, I am not able to get those type of fields added, to make them available for tokens, even via relationships. Maybe there is a better way to build the view?

tonytheferg

Add taxonomy term in popup

1 month 3 weeks ago

I have a select list with some options. But now I also want an option Other. And when you select the Other option there has to open a pop-up screen with the add term to vocabulary form in it. The term should not be added directly to the vocabulary, it should be "disabled" first so the administrator can approve them.

I know how to get the taxonomy form (taxonomy_form_term, drupal_get_form, drupal_render_form) but I have some other questions:

  • How can I open the popup and show call the function in my module to render the form? Should I add another option to my select list?
  • How can I make sure when you add it, it doesn't add directly to the vocabulary (admin has to click approve)
nielsv

how to print a form in twig where a variable number of form elements have been printed already?

1 month 3 weeks ago

I am using a Twig template to render a form that has a variable number of elements.

Each element's key in the form is sequential integer.

So in my template, I can do this:

{% for item in form|children %} {{ item }} {% endfor %}

However, then I have a broken form, because there's no <FORM> HTML element, or the form ID for submission.

I've looked at twig templates for forms in core, but they all work with fixed elements, so they're able to do something like this, seen in modules/content_moderation/templates/entity-moderation-form.html.twig:

{{ form|without('current', 'new_state', 'revision_log', 'submit') }}
joachim

How do I use print to generate a PDF of all entities referencing a specific node?

1 month 3 weeks ago

I'm trying to use the Print module to generate pdf's and epubs of the stories on my site. However, I need to get Print to generate these files from the story page and then generate the pdf with ALL nodes that reference the story, the chapters. I'm not sure how to do this as I've never really done tpl.php's before. I need some help here.

Can print be made to work with a panels layout that shows what I want in it? I have a panels layout working mostly for an example of what I'm trying to achieve.

http://fanbards.net/print/stories/13008

How do I make the tpl.php file I need to make that end up as my print selections for Stories?

CW Smith