أنا أبحث عن مقتطف يتيح لي عرض العدد الإجمالي لنوع عقدة معين ، على سبيل المثال "Pages = 167" أو "Products = 10630."
ما هو الرمز الذي يجب أن أستخدمه لتحقيق ذلك؟
فيما يلي دالة ستعرض عدد العقد لنوع محتوى معين:
function YOURTHEME_get_node_count($content_type) {
$query = 'SELECT COUNT(*) ' .
'FROM {node} n ' .
'WHERE n.type = :type';
return db_query($query, array(
':type' => $content_type
))->fetchField();
}
لاستخدام هذا الرمز في السمة ، أضف الوظيفة إلى template.php
وبعد ذلك يمكنك استدعاء الوظيفة على النحو التالي:
echo 'Pages: ' . YOURTHEME_get_node_count('page');
echo 'Products: ' . YOURTHEME_get_node_count('product');
يمكنك استخدام الوحدة طرق العرض للقيام بذلك.
يجب أن يكون عليه! إذا لزم الأمر ، اضبط المزيد من الإعدادات مثل التسميات الميدانية وإعدادات نمط الصف.
فيما يلي تصدير لمثل هذا العرض ، بحيث يمكنك استيراده وتجربته بسهولة:
$view = new view;
$view->name = 'nodecounts';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Node counts';
$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'] = 'Node counts';
$handler->display->display_options['group_by'] = TRUE;
$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'] = 'none';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
$handler->display->display_options['row_options']['inline'] = array(
'type_1' => 'type_1',
'type' => 'type',
);
$handler->display->display_options['row_options']['separator'] = ': ';
$handler->display->display_options['row_options']['hide_empty'] = 0;
$handler->display->display_options['row_options']['default_field_elements'] = 1;
/* Field: Content: Type */
$handler->display->display_options['fields']['type_1']['id'] = 'type_1';
$handler->display->display_options['fields']['type_1']['table'] = 'node';
$handler->display->display_options['fields']['type_1']['field'] = 'type';
$handler->display->display_options['fields']['type_1']['label'] = '';
$handler->display->display_options['fields']['type_1']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['external'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['Word_boundary'] = 1;
$handler->display->display_options['fields']['type_1']['alter']['Ellipsis'] = 1;
$handler->display->display_options['fields']['type_1']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['trim'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['html'] = 0;
$handler->display->display_options['fields']['type_1']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['type_1']['element_default_classes'] = 1;
$handler->display->display_options['fields']['type_1']['hide_empty'] = 0;
$handler->display->display_options['fields']['type_1']['empty_zero'] = 0;
$handler->display->display_options['fields']['type_1']['hide_alter_empty'] = 1;
$handler->display->display_options['fields']['type_1']['link_to_node'] = 0;
$handler->display->display_options['fields']['type_1']['machine_name'] = 0;
/* Field: COUNT(Content: Type) */
$handler->display->display_options['fields']['type']['id'] = 'type';
$handler->display->display_options['fields']['type']['table'] = 'node';
$handler->display->display_options['fields']['type']['field'] = 'type';
$handler->display->display_options['fields']['type']['group_type'] = 'count';
$handler->display->display_options['fields']['type']['label'] = '';
$handler->display->display_options['fields']['type']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['type']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['type']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['type']['alter']['external'] = 0;
$handler->display->display_options['fields']['type']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['type']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['type']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['type']['alter']['Word_boundary'] = 1;
$handler->display->display_options['fields']['type']['alter']['Ellipsis'] = 1;
$handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['type']['alter']['trim'] = 0;
$handler->display->display_options['fields']['type']['alter']['html'] = 0;
$handler->display->display_options['fields']['type']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['type']['element_default_classes'] = 1;
$handler->display->display_options['fields']['type']['hide_empty'] = 0;
$handler->display->display_options['fields']['type']['empty_zero'] = 0;
$handler->display->display_options['fields']['type']['hide_alter_empty'] = 1;
$handler->display->display_options['fields']['type']['separator'] = '';
$handler->display->display_options['fields']['type']['format_plural'] = 0;
/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block');
الطريقة الآلية المبرمجة هي استخدام EntityFieldQuery class . تعرف على سبب EntityFieldQuery أعلى من db_query () .
فيما يلي مثال لعد العقد من نوع المدونة.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node') // grab nodes
->entityCondition('bundle', 'blog') // filter by blog type
->propertyCondition('status', 1) // filter by published
->count(); // count
$result = $query->execute();
انظر سؤال مشابه .
فعلت ذلك باستخدام EntityFieldQuery.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
/* this is the content type machine name */
->entityCondition('bundle', 'product')
/* You can set extra properties using fieldCondition and properties with propertyCondition */
->fieldCondition('field_product_status', 'tid', key(taxonomy_get_term_by_name('New')))
;
$result = $query->execute();
if (isset($result['node'])){
$count_of_new_product_nodes = count($result['node']);
}
استخدام Drush بسيط وسريع.
drush sqlq 'select count(node.nid) as node_count, node_type.type from node inner join node_type on node.type = node_type.type group by node_type.type'
وهذا يعطي نتائج مشابهة لما يلي:
node_count type
17 category_2012
20 category_2013
19 category_2014
3 competition
19 entry_2012_breakthrough
89 entry_2012_digitalother
50 entry_2012_directdirect
19 entry_2012_filmsecscn
17 entry_2012_insights
12 entry_2012_outdoor
31 entry_2012_promo
19 entry_2013_breakthrough
100 entry_2013_digitalother
40 entry_2013_directdirect
ثم إذا كنت تريد التصفية حسب نوع معين ، فما عليك سوى استخدام grep مثل هذا:
drush sqlq 'select count(node.nid) as node_count, node_type.type from node inner join node_type on node.type = node_type.type group by node_type.type' | grep 2014
بالنسبة لأي شخص مهتم ، هناك حل آخر وهو استخدام countQuery طريقة SelectQuery class (عبر db_select ).
$count = db_select('node')
->condition('type', 'some-type')
->countQuery()->execute()->fetchField();
ومع ذلك ، أنا أفضل حل EntityFieldQuery الذي نشرته timofey. أنا أقدم هذا فقط كبديل معقول إلى حد ما.
SELECT
COUNT({node}.nid) AS node_count,
{node_type}.type
FROM {node}
INNER JOIN {node_type} ON {node}.type = {node_type}.type
GROUP BY {node_type}.type;
استخدم هذا الاستعلام في التعليمات البرمجية الخاصة بك
كتنوع للإجابة عن استخدام الوحدة طرق العرض ، يمكنك "استخدام" العرض الذي يأتي مع وحدة الرسوم البيانية . ببساطة قم بتثبيته/تمكينه ، ولا حاجة إلى تكوين إضافي أو الترميز ، إلخ. مزيد من التفاصيل حول هذا العرض ، المدرجة في أمثلة من خارج الصندوق (اقتباس من هذا الرابط):
... انتقل إلى
charts/examples/views
في موقعك. ثم سترى مخططًا عموديًا ومخططًا دائريًا ، يتبعهما عرض جدولي أيضًا. يحتوي كل من المخططات وشاشة الجدول على بيانات حول إجمالي عدد العقد لكل نوع من أنواع المحتوى المتاحة.
ملاحظات:
الإفصاح: أنا المشرف على هذه الوحدة ،
آمل ألا يخالف هذا الموقع سياسة الترويج الذاتي .
عدد نوع العقدة الوحدة النمطية تفعل نفس الشيء الذي تطلبه.
تستخدم هذه الوحدة لإظهار عدد العقد من نوع محتوى معين وكذلك عدد المستخدمين من نوع دور معين.
سيتم استخدام هذه الوحدة لأغراض إحصائية ومتخصصة فقط.