How to get total rows in Drupal 8 view with pagination? - drupal

In Drupal 8, I use a view with pagination. There are 10 items per page.
In hook_views_pre_render(ViewExecutable $view) {..} I want to get the number of items to use in another function. However, $result = 10 and $total_rows = 10 due to the pager setting. The actual item number is between 500 and 2000.
As a workaround I tried a temporary view.
$view2 = Views::getView('my_view');
$view2->setDisplay('page_1');
$view2->setItemsPerPage(15000);
$view2->execute();
This works. view2->total_rows returns the desired value. But I don't want to use a fixed value in setItemsPerPage. 0 or null (for unlimited items per page) returns no result for total_rows.
What can I do to get the correct number of views-items?

In your hook_views_pre_render function, instead of calling view->SetDisplay and view->setItemsPerPage, you can execute the view (even with pager settings) and get the total rows/results:
$view = Views::getView('my_view');
$view->execute('page_1');
$rows = $view->total_rows;
$rows will provide the total count that you are looking for.

And get_total_rows needs to be set to true for total_rows to operate.
$view = Views::getView('my_view');
$view->get_total_rows = TRUE;
$view->execute('page_1');
$rows = $view->total_rows;

Related

Gravity forms conditional logic based on number of values selected

I have a form built with gravity forms that asks 5 questions, each with 2 answers.
Each answer corresponds to a category, and so I want to be able to add some conditional logic so that the confirmation sends to a different page depending on which category has more answers.
Form is here: https://90daysfromretirement.com/medicare-advantage-vs-medicare-supplement/
The two categories are "Advantage" and "Supplement" - so dependng on the answer, so whichever category has more responses (3 or greater) then the confirmation will redirect them to the appropriate page.
Problem is I can't figure out how to Count the values for each and run logic off it. I don't think it's possible with any gravity forms add-ons I've found, and I haven't been able to put the code together to do it.
Any suggestions?
Since you only have two options, you really only need to know the count of one and the total number of questions to know which value has been selected more.
For example, if you have 10 questions and "Advantage" choices are selected 6 times, you know that Advantage has more than "Supplement".
Here's a snippet that should get you started. It will take a group of Radio Button fields, count the number of times a specified value has been selected, and populate that count into a Number field.
You can then use that Number field to apply conditional logic on your confirmations to redirect to the appropriate page (e.g. If Count is greater than 5, redirect to Advantage page. If Count is less than 6, redirect to Supplement page).
/**
* Gravity Wiz // Gravity Forms // Value Counter
* https://gravitywiz.com/
*
* Count the number of times a given value has been selected in a group of fields and populate that number into a Number field.
* This snippet is designed to target a Number field and count selected values in Checkbox and Radio Button fields.
*
* This snippet works best with our free [GF Custom Javascript](https://gravitywiz.com/gravity-forms-custom-javascript/) plugin.
*/
// Replace the "1", "2" and "3" with field IDs of fields that should have their selected values counted. If you are using the
var $radios = jQuery( '#field_GFFORMID_1, #field_GFFORMID_2, #field_GFFORMID_3' );
// Replace "4" with the ID of the Number field in which the count should be populated.
var $target = jQuery( '#field_GFFORMID_4' );
// Replace "a" with the value you wish to count if selected.
var countValue = 'a';
function gwRecountValues() {
$target.find( 'input' ).val( $radios.find( 'input:checked[value="' + countValue + '"]' ).length );
}
$radios.on( 'change', function() {
gwRecountValues();
} );
gwRecountValues();
And here's the source which may be updated in the future if other folks use this and experience any issues or need improvements: https://github.com/gravitywiz/snippet-library/commit/d34eb169da937981c4a1ea49bb8a36df023bff1b
The one thing missing from this snippet is PHP validation. You may not be concerned about people altering their count but it is possible without PHP validation.
You could try adding the following to your functions.php file
add_action( "gform_after_submission_66", "after_submission", 10, 2 ); //replace 66 with your form number
function after_submission($entry, $form ){
$supplement_answers = array("milk", "apple", "pasta", "fries", "oats"); //replace array items with the supplement answers
$advantage_answers = array("red", "blue", "pink", "purple", "yellow"); //replace array items with the advantage answers
$field_nums = array("2","3","6","7","9"); //replace these field numbers with your own -- order then to align with answers above
$count_fields = count($field_nums);
$a = 0;
$b = 0;
for($i=0; $i<$count_fields; $i++){
if($entry[$field_nums[$i]] == $supplement_answers[$i]){
$a = $a + 1;
}else if($entry[$field_nums[$i]] == $advantage_answers[$i]){
$b = $b + 1;
}
}
if($a > $b){
header('Location: https://website/supplements_page'); //replace url
}else if($a < $b){
header('https://website/advantage_page'); //replace url -
}
}

How to get total number of rows in a drupal view alter hook?

I've to avoid duplicate search result in a view, so what I am trying to alter the view using pre render hook. and removing the duplicates it's working fine but the problem is in the count of result. it shows the count from the query executed and this include the duplicated item too. also, I enabled the pagination with limit of 5 in a page. then the count seems to be strange it's taking the count of the elements showing in each page
function search_helper_views_pre_render(\Drupal\views\ViewExecutable $view) {
if ($view->id() == "all_news" || $view->id() == "all_publications" || $view->id() == "all_events" || $view->id() == "global_search") {
$unique_nids = $d_nids = $new_results = array();
// Loop through results and filter out duplicate results.
foreach($view->result as $key => $result) {
if(!in_array($result->nid, $unique_nids)) {
$unique_nids[] = $result->nid;
}
else {
unset($view->result[$key]);
}
}
$view->total_rows = count($view->result);
//$view->pager->total_items = count($view->result);
$view->pager->updatePageInfo();
}
}
the expected output of the $view->total_rows must be the total count of result instead of count of elements shown in the page.
You totaly done it in wrong way. as you see ( and it's clear from its name ), it's hook__views_pre_render it runs before the rendering. So its really hard to manipulate the views results and counter, pagination there.
As I see in your query you just remove duplicate Nids , so you can easily do it by Distinct drupal views feature.
Under Advanced, query settings, click on settings.
You will get this popup, now checkmark Distinct
Could do
$difference = count($view->result) - count($new_result);
$view->total_rows = $view->total_rows - $difference;
BTW Distinct setting doesn't always work, see https://www.drupal.org/project/drupal/issues/2993688

Xamarin grid, column and row amounts

Hi im relatively new to c# code and i was wondering if there is any way to get the amount of columns and rows in a grid and store that amount in a variable
Something like:
var columnamount = grid.columnamount;
But i could not find anything that works
Thanks
You can use the following code to get a count of the columns and rows directly via the ColumnDefinitions and RowDefinitions properties. No need to enumerate the children of the grid because you may not have views in every column/row.
var columnCount = grid.ColumnDefintions.Count;
var rowCount = grid.RowDefinitions.Count;
For reference the documentation.
You might be able to do it this way, purely based on what I see in the docs:
var countColumns = grid.Children.Where( c => c.Column).Max();
var countRows = grid.Children.Where( c => c.Row).Max();
But I'm not sure if you can access Row anf Column properties on the child element.
This is not the best way to check, I guess, but it's working (same thing for columns):
EDIT: nope, for columns it doesn't work
int GetRowsCount(Grid grid)
{
var item = grid.Children.FirstOrDefault();
return item != null ? Grid.GetRow(item) + 1 : 0;
}

Woocommerce: How-to show ‘Order Total’ amount in Words

On my Woocommerce website, I need to display Total Order Amount in words, which will be shown on Checkout Page, Cheque Payment & on Invoice.
Example: 1590.00 (One thousand five hundred & ninety only)
How can we achieve this?
TIA
You can try number formatter class as mentioned in these threads a and b
Use the filter "woocommerce_cart_totals_order_total_html".
function custom_woocommerce_order_amount_total( $order_total ) {
$number_total = WC()->cart->get_total();
// number formatting goes here;
// using number formatter class
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
$total_in_words = $f->format($number_total); // Total in words
$order_total = $total_in_words;
return $order_total;
};
add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_woocommerce_order_amount_total' );
You can also try the other hooks like woocommerce_get_formatted_order_total

Drupal return number of results in a View

I have a view in Drupal that filters my content. It brings back 7 rows. All I want to return is the number or results returned(7). Is this possible?
I tried using the View result counter but it returns a number for each results
1
2
3
4
5
6
7
I just need the 7 part.
So in SQL I would do a select count(*)
what you can do is to activate php for the views header/footer and add the following snippet to it:
<?php
$view = views_get_current_view();
print $view->total_rows;
?>
This will print the total number of rows.
If you need the result as a field, you could use the "Views custom field" module, add a php field and run the same snippet.
Regards
Mike
If you want to get the count outside the view you can use this
$view = views_get_view('MY_VIEW_NAME');
$view->set_display('MY_DISPLAY'); // like 'block_1'
$view->render();
print sizeof($view->result);
Note : Do not use this within a view. It will be an overhead. If you are using it within view check the other answers.
If using views_get_view in Views 3, you can use this snippet:
$view = views_get_view('MY_VIEW');
$view->set_display('MY_DISPLAY');
// Execute first
$result = $view->preview('MY_DISPLAY');
// Output result only if rows > 0
if (count($view->result) > 0) {
print $result;
}
This works well for me and deals with the pager issues. Put this function in your custom module, rename / format as needed, and call it from your views-view--*view_name_goes_here*.tpl.php files.
function get_view_rowcount(){
$view = views_get_current_view();
$page_total = count($view->result);
if(isset($view->total_rows)){
return "<strong>Displaying " . $page_total . " of " . $view->total_rows . " total rows.</strong>";
} else {
return "<strong>Displaying " . $page_total . " of " . $page_total . " total rows.</strong>";
}
}
With Views 3 you may need to do
$view->get_total_rows = TRUE;
$total_items = $view->query->pager->get_total_items();
with drupal 7 - Under the pager options you have the option to
Expose items per page
When checked, users can determine how many items per page show in a view

Resources