Drupal StackExchange

Replace textfield value on ajax callback

13 hours 49 minutes ago

1) Am i missing something here? it's not replacing the textfield value.
2) Also how do i replace if i have a multiple textfield and multiple button in a form.

<?php function test($form, &$form_state) { if (isset($form_state['values']['your_name'])) { $name_value = $form_state['values']['your_name']; } else { $name_value = ""; } $form = array(); $form['your_name'] = array( '#title' => t('Your or your company name'), '#type' => 'textfield', '#value' => $name_value, '#prefix' => '<div id ="subscribers-name">', '#suffix' => '</div>', ); $form['load_settings'] = array( '#type' => 'button', '#value' => t('Load my settings'), '#ajax' => array( 'wrapper' => 'subscribers-name', 'callback' => 'simplenews_mod_load_subscriptions_ajax', 'method' => 'replace', ) ); return $form; } function simplenews_mod_load_subscriptions_ajax($form, &$form_state) { $form_state['values']['your_name'] = "loaded value"; return $form['your_name']; }

How to set body field display programmatically on node type install?

15 hours 44 minutes ago

When hook_installing a new custom node type filled with custom fields I call field_create_instance($instance) where I also set the custom fields' display like so:

'display' => array( 'default' => array( // view mode 'label' => 'hidden', 'type' => 'text_default' ), 'teaser' => array( // view mode 'label' => 'hidden', 'type' => 'hidden' ) )

I also added a body field via node_add_body_field($type) but where can I set that body's display properties programmatically? Per default it's set to be displayed in the teaser view. I want to hide it.


Create input name from array like #parent

15 hours 56 minutes ago

I'm just wondering if there is a Drupal way / helper function to build input[name] string from array.

So I have an array '#parents' = ["info","variation-555-1","example","example"];

and I want the string to go in the input [name=info[variation-555-1][example][example]]


How can I add asc and desc sort symbols?

16 hours 47 minutes ago

I have a drupal 7 view, consists of table. I need to add sort symbol (both asc and desc) in view header by default if no sorting applied.

I tried with altering views-view-table.tpl.php file, and added asc, desc images for each header.

But I need proper solution, Is there any hook there to alter the table headers?


How to filter a view based for the current user based the value of custom fields in his profile and fields in the list of items viewed

18 hours 46 minutes ago

I'd like to implement Views that conditionally show or hide item listings of content nodes to the current User based on:
(1) value(s) in the current User's profile and
(2) value(s) in the profile of the author of each item and
(3) value(s) in each content node itself.

A good example would be a classified ad system that enabled advertisers to specify that a given ad be shown only to users whose profiles meet a set of specific criteria.

For example: A Drupal site with User Accounts with custom fields to capture each user's age, height, and hair, and a Content Type for advertisements with the same fields.

The challenge is how to construct a View that supports a use case like the following: Users who will look at the View:
= David is a bald, 6.5 foot, 70 yr old.
= Edward is a blond, 5'5", 18 year old.
= Fred is a brown-haired, 5'11", 30 year old.
Ads that can be displayed in the View as follows:
= "Miracle Grow Hair Spray" posted by Abby to be shown only to bald people.
= "NuEar Hearing Aids" posted by Betty to be shown only to people over age 65.
= "Big and Tall Shop" posted by Carol only to be shown to people over 6 feet tall.

In summary, I'm trying to filter tablular View of nodes based on attributes of the content nodes, the authors of the node, and the current user who is looking at the View.
The standard Views filter UI supports filters based on attributes of each node in the table, but does not appear to permit any use of information about the current user other than his login state.

Any advice and references to doing the filtering at the granularity of each node would be greatly appreciated!


What would cause config export (drush cex) to output to multiple folders instead of just config/sync?

19 hours 4 minutes ago

settings.php has a single config sync setting:

$settings['config_sync_directory'] = '../config/sync';

Yet every time we run "drush cex" it writes the same config .yml files to three places:

/config /config/sync /config/sync/sync

This is baffling! I have many sites using config sync and cannot figure out why we are getting this odd behaviour.


AJAX loading of a view on click of a button?

19 hours 44 minutes ago

I want to switch between two methods of sorting and displaying items in Views by toggling a button. My exposed filters are in the URL so I want to include the query in the path in order to keep the exposed filter results. Both displays of the view have "use ajax" set to "yes".

My link is :

$link_path = ($display_data->display_options['path'] . '/nojs'); $query = drupal_get_query_parameters(); $toggle_link = l($toggle_icon, $link_path, array('html' => true, 'query' => $query, 'attributes' => array('class' => array('use-ajax'))));

Which outputs something like:

<a href="href="/my_view_page_1/nojs?terms=test&param1[0]=123">[omitted SVG path for brevity]</a>

In a js file in my theme, I have:

/** * Toggle button and ajax views */ var viewId = settings.mytheme_toggle_view.view; var viewDisplayId = settings.mytheme_toggle_view.current_display; var viewOtherDisplayId = settings.mytheme_toggle_view.other_display; $.ajax({ url: settings.basePath + 'views/ajax/' + settings.mytheme_toggle_view.query, type: 'post', data: { view_name: viewId, view_display_id: viewOtherDisplayId, view_args: {}, // your views arguments }, dataType: 'json', success: function (response) { var newView = ($(response[1].data)); $(".view-display-id-" + viewDisplayId).replaceWith($(newView)); } }); /* End views ajax stuff. */

What happens now:

  1. I go to my view and select my exposed filters and hit "apply"
  2. I arrive at my view's default display page
  3. Before I can click anything, AJAX fires and replaces the view content with the alternative view content.
  4. If I click the toggle, it is a link (with full page reload, not AJAX) to the default display page again.
  5. Step 3 repeats.

What I want is for the view to

  1. Not change on pageload, but wait until the toggle button is clicked
  2. Be able to be toggled back and forth rather than only toggling once and then becoming a regular, page-reloading link.
  3. Preserve the exposed filters
  4. Have the pager still work (currently is a link to some JSON output of the settings object).

List (distinct) users in order of their most recently published node

20 hours 35 minutes ago

I'm trying to list 8 users (name and profile image) starting with the one who created the newest node and follow with the rest of users in (descending) order of their last created node.

I got to achieve it* by:

  1. adding fields and filters
  2. adding a Relationship (advanced) with Content authored
  3. adding Sort criteria as Content: date (descendant)

It 'works' but I get duplicated users. If a user created the 3 newest nodes, then it appears in positions #1, #2 and #3. I can't get it to get only distinct (unique) users in the list. Aggregation did not work.

Any clues? Ty


Add prefix / suffix to field by using template_preprocess_field

20 hours 50 minutes ago

I remember in Drupal 7, this was possible. I need to add some HTML before a field and I don't want to edit it in the Twig file. Is it somehow possible to add prefixes / suffixes by using the template_preprocess_field function?

I already tried something like that, but with no success:

function MYTHEME_preprocess_field(&$variables, $hook) { $element = $variables['element']; if ($element['#field_name'] === 'field_genre') { $element[0]['#prefix'] = '<div class="project-info">'; } if ($element['#field_name'] === 'field_duration') { $element[0]['#suffix'] = '</div>'; } }