6

How can I programmatically insert values into Global custom text field from the views. I have tried using the $views variable from the hook_views_pre_render(&$views) and my_theme_name_process_function(&$vars) but was unsucessful in getting the value ? When I try for a simple field I can insert html into the markup programmatically.

$view = view Object
(
    [db_table] => views_view
    [base_table] => node
    [base_field] => nid
    [name] =>view_name
    [vid] => 3
    [description] => 
    [tag] => default
    [human_name] => view_name
    [core] => 7
    [api_version] => 
    [disabled] => 
    [editing] => 
    [args] => Array
    (
        )

    [use_ajax] => 
    [result] => Array

  [result] => Array
        (
            [0] => stdClass Object
                   [field_field_final_ort] => Array
                            (
                                [0] => Array
                                    (
                                        [rendered] => Array
                                            (
                                                     ['#markup'] => YES     

I want to similarly change the value of GLOBAL custom text field using views object but there is not such array for global custom text field.
What is the way for changing it?

My code was

function my_themename_process_views_view_field(&$vars) {

  if ($vars['view']->name == 'view_name') {
foreach( $vars['view']->result as $key => $value) { 
$value->field_name[0]['rendered']['#markup'] = '';
}

}

I want to apply this similar code for global custom text field but there is no suxh available inside result array

How can I alter values for each row of global custom text field value ? The answer given below gives same values for each row .

2

1 Answer 1

14

You should be able to do that with hook_views_pre_render(). Please try the following code:

function YOUR_THEME_views_pre_render(&$view) {

  switch ($view->name) {
    case 'YOUR_VIEW_NAME':
      //  override the global custom text field value
      $view->field['nothing']->options['alter']['text'] = 'My custom text';
    break;
  }
}

If the global custom text value need to be different for each view result, you can configure it using replacement patterns.

enter image description here

In order to programmatically insert values into Global custom text field, You can use template_preprocess_views_view_fields()

function YOUR_THEME_preprocess_views_view_fields(&$vars) {
  $view = $vars['view'];

  foreach ($view->field as $id => $field) {

    // The global custom text content
    $vars['fields']['nothing']->content = 'Some global custom text content';

    // change the global custom text content depend on result (node id)
    if ($vars['row']->nid == 10) $vars['fields']['nothing']->content = 'CHANGED';
  }
}
8
  • How can I alter values for each row of global custom text field value ?
    – harshal
    Sep 4, 2013 at 5:32
  • Please specify what exactly markup are you going to create.
    – Jack-PL
    Sep 4, 2013 at 15:58
  • I am adding nid using node_load($nid) which is placed inside foreach loop so due to looping all global custom text value is assigned with the last nid value in the loop.
    – harshal
    Sep 5, 2013 at 3:36
  • Is there any reason to not use replacement patterns in this global field configuration? I edited my answer and added an example.
    – Jack-PL
    Sep 5, 2013 at 19:44
  • Thanks.. Yes Actually I am getting a value from a custom function and depending on that I am setting the value. So I cannot use replacement pattern. I know about views php but then I want a solution which will provide me a different values assigned to global custom text based on condition in the code.
    – harshal
    Sep 6, 2013 at 3:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.