/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('uc_catalog.settings'); $view = Views::getView('uc_catalog'); $view->initDisplay(); $displays = array(); foreach ($view->displayHandlers as $display) { if ($display->getPluginId() == 'page') { $displays[$display->display['id']] = $display->display['display_title']; } } $form['uc_catalog_display'] = array('#type' => 'select', '#title' => $this->t('Catalog display'), '#default_value' => $config->get('display'), '#options' => $displays); $vid = $config->get('vocabulary'); if ($vid) { $catalog = Vocabulary::load($vid); $form['catalog_vid'] = array('#markup' => '<p>' . $this->t('The taxonomy vocabulary <a href=":edit-url">%name</a> is set as the product catalog.', [':edit-url' => Url::fromRoute('entity.taxonomy_vocabulary.edit_form', ['taxonomy_vocabulary' => $catalog->id()])->toString(), '%name' => $catalog->label()]) . '</p>'); } $vocabs = array(); $vocabularies = Vocabulary::loadMultiple(); foreach ($vocabularies as $vid => $vocabulary) { $vocabs[$vid] = $vocabulary->label(); } $form['uc_catalog_vid'] = array('#type' => 'select', '#title' => $this->t('Catalog vocabulary'), '#default_value' => $config->get('vocabulary'), '#options' => $vocabs); $form['uc_catalog_breadcrumb'] = array('#type' => 'checkbox', '#title' => $this->t('Display the catalog breadcrumb'), '#default_value' => $config->get('breadcrumb')); return parent::buildForm($form, $form_state); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $faq_settings = $this->config('faq.settings'); // Set up a hidden variable. $form['faq_display'] = array('#type' => 'hidden', '#value' => $faq_settings->get('display')); $form['faq_use_categories'] = array('#type' => 'checkbox', '#title' => $this->t('Categorize questions'), '#description' => $this->t('This allows the user to display the questions according to the categories configured on the add/edit FAQ page. Use of sub-categories is only recommended for large lists of questions. The Taxonomy module must be enabled.'), '#default_value' => $faq_settings->get('use_categories')); $category_options['none'] = t("Don't display"); $category_options['categories_inline'] = $this->t('Categories inline'); $category_options['hide_qa'] = $this->t('Clicking on category opens/hides questions and answers under category'); $category_options['new_page'] = $this->t('Clicking on category opens the questions/answers in a new page'); $form['faq_category_display'] = array('#type' => 'radios', '#options' => $category_options, '#title' => $this->t('Categories layout'), '#description' => $this->t('This controls how the categories are displayed on the page and what happens when someone clicks on the category.'), '#default_value' => $faq_settings->get('category_display')); $form['faq_category_misc'] = array('#type' => 'details', '#title' => $this->t('Miscellaneous layout settings'), '#open' => TRUE); $form['faq_category_misc']['faq_category_listing'] = array('#type' => 'select', '#options' => array('ol' => $this->t('Ordered list'), 'ul' => $this->t('Unordered list')), '#title' => $this->t('Categories listing style'), '#description' => t("This allows to select how the categories listing is presented. It only applies to the 'Clicking on category opens the questions/answers in a new page' layout. An ordered listing would number the categories, whereas an unordered list will have a bullet to the left of each category."), '#default_value' => $faq_settings->get('category_listing')); $form['faq_category_misc']['faq_category_hide_qa_accordion'] = array('#type' => 'checkbox', '#title' => $this->t('Use accordion effect for "opens/hides questions and answers under category" layout'), '#description' => $this->t('This enables an "accordion" style effect where when a category is clicked, the questions appears beneath, and is then hidden when another category is opened.'), '#default_value' => $faq_settings->get('category_hide_qa_accordion')); $form['faq_category_misc']['faq_count'] = array('#type' => 'checkbox', '#title' => $this->t('Show FAQ count'), '#description' => $this->t('This displays the number of questions in a category after the category name.'), '#default_value' => $faq_settings->get('count')); $form['faq_category_misc']['faq_answer_category_name'] = array('#type' => 'checkbox', '#title' => $this->t('Display category name for answers'), '#description' => t("This allows the user to toggle the visibility of the category name above each answer section for the 'Clicking on question takes user to answer further down the page' question/answer display."), '#default_value' => $faq_settings->get('answer_category_name')); $form['faq_category_misc']['faq_group_questions_top'] = array('#type' => 'checkbox', '#title' => t("Group questions and answers for 'Categories inline'"), '#description' => t("This controls how categories are implemented with the 'Clicking on question takes user to answer further down the page' question/answer display."), '#default_value' => $faq_settings->get('group_questions_top')); $form['faq_category_misc']['faq_hide_child_terms'] = array('#type' => 'checkbox', '#title' => $this->t('Only show sub-categories when parent category is selected'), '#description' => t("This allows the user more control over how and when sub-categories are displayed. It does not affect the 'Categories inline' display."), '#default_value' => $faq_settings->get('hide_child_terms')); $form['faq_category_misc']['faq_show_term_page_children'] = array('#type' => 'checkbox', '#title' => $this->t('Show sub-categories on FAQ category pages'), '#description' => t("Sub-categories with 'faq' nodes will be displayed on the per category FAQ page. This will also happen if 'Only show sub-categories when parent category is selected' is set."), '#default_value' => $faq_settings->get('show_term_page_children')); $moduleHandler = \Drupal::moduleHandler(); if ($moduleHandler->moduleExists('taxonomy')) { $form['faq_category_advanced'] = array('#type' => 'details', '#title' => $this->t('Advanced category settings'), '#open' => FALSE); $vocab_options = array(); $vocabularies = Vocabulary::loadMultiple(); foreach ($vocabularies as $vid => $vobj) { $vocab_options[$vid] = $vobj->name; } if (!empty($vocab_options)) { $form['faq_category_advanced']['faq_omit_vocabulary'] = array('#type' => 'checkboxes', '#title' => $this->t('Omit vocabulary'), '#description' => $this->t('Terms from these vocabularies will be <em>excluded</em> from the FAQ pages.'), '#default_value' => $faq_settings->get('omit_vocabulary'), '#options' => $vocab_options, '#multiple' => TRUE); } } return parent::buildForm($form, $form_state); }
/** * Implements \Drupal\block\BlockBase::blockBuild(). */ public function build() { static $vocabularies, $terms; $items = array(); $faq_settings = \Drupal::config('faq.settings'); if (!$faq_settings->get('use_categories')) { return; } $moduleHandler = \Drupal::moduleHandler(); if ($moduleHandler->moduleExists('taxonomy')) { if (!isset($terms)) { $terms = array(); $vocabularies = Vocabulary::loadMultiple(); $vocab_omit = array_flip($faq_settings->get('omit_vocabulary')); $vocabularies = array_diff_key($vocabularies, $vocab_omit); foreach ($vocabularies as $vocab) { foreach (taxonomy_get_tree($vocab->vid) as $term) { if (FaqHelper::taxonomyTermCountNodes($term->tid)) { $terms[$term->name] = $term->tid; } } } } if (count($terms) > 0) { foreach ($terms as $name => $tid) { $items[] = l($name, 'faq-page/' . $tid); } } } return array('#theme' => 'item_list', '#items' => $items, '#list_type' => $faq_settings->get('category_listing')); }
/** * {@inheritdoc} */ public function buildOptionsForm(&$form, FormStateInterface $form_state) { parent::buildOptionsForm($form, $form_state); $form['title_field'] = array('#type' => 'select', '#title' => $this->t('Title field'), '#options' => $this->displayHandler->getFieldLabels(TRUE), '#required' => TRUE, '#default_value' => $this->options['title_field'], '#description' => $this->t('Select the field that will be used as the title.')); $form['accordion_filter'] = array('#type' => 'select', '#title' => t('Use Filter'), '#options' => array(0 => t('No'), 1 => t('Yes')), '#description' => t('Filter items by taxonomy term'), '#default_value' => $this->options['accordion_filter'], '#attributes' => array('class' => array('accordion-filter-option'))); $categories = array(); $categories['select'] = t('Select'); foreach (Vocabulary::loadMultiple() as $vocabulary) { $categories[$vocabulary->id()] = $vocabulary->get('name'); } $form['accordion_filter_vocabulary'] = array('#type' => 'select', '#title' => t('Filter Vocabulary'), '#options' => $categories, '#description' => t('Which taxonomy vocabulary do you want to use for the filter'), '#default_value' => $this->options['accordion_filter_vocabulary'], '#states' => array('visible' => array('.accordion-filter-option' => array('value' => 1)))); }
/** * {@inheritdoc} */ public function settingsForm($form, FormStateInterface $form_state) { $config = $this->getConfiguration(); $names = taxonomy_vocabulary_get_names(); $vocabularies = Vocabulary::loadMultiple($names); // Should use dependency injection rather. $options = array(); foreach ($vocabularies as $vocabulary) { $options[$vocabulary->id()] = $vocabulary->label(); } $settings['vocabulary'] = array('#type' => 'select', '#title' => t('Vocabulary'), '#default_value' => $config['vocabulary'], '#options' => $options); return $settings; }
/** * Form constructor. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * * @return array * The form structure. */ public function buildForm(array $form, FormStateInterface $form_state) { $sitemap = \Drupal::service('custom_sitemap.sitemap'); $entity_types = $sitemap->get_entity_types(); $custom_links = $sitemap->get_custom_links(); $form = array('entity-types' => array('#type' => 'vertical_tabs'), 'node' => array('#type' => 'details', '#title' => $this->t('Content types'), '#group' => 'entity-types'), 'taxonomy_term' => array('#type' => 'details', '#title' => $this->t('Vocabularies'), '#group' => 'entity-types')); /** @var NodeType[] $content_types */ $content_types = NodeType::loadMultiple(); $this->setEntityTypeDetailForm($form, 'node', $content_types, $entity_types['node']); /** @var Vocabulary[] $vocabularies */ $vocabularies = Vocabulary::loadMultiple(); $this->setEntityTypeDetailForm($form, 'taxonomy_term', $vocabularies, $entity_types['taxonomy_term']); return parent::buildForm($form, $form_state); }
/** * {@inheritdoc} */ public function buildOptionsForm(&$form, FormStateInterface $form_state) { parent::buildOptionsForm($form, $form_state); $form['grid_style'] = array('#prefix' => '<h4>Grid Settings</h4>', '#type' => 'select', '#title' => t('Mode'), '#description' => t('Choose grid style:'), '#options' => array('classic' => t('Classic Grid'), 'masonry' => t('Masonry Simple'), 'masonry_resize' => t('Masonry Resize')), '#default_value' => $this->options['grid_style'], '#attributes' => array('class' => array('grid-style'))); $field_options = array(); $fields = \Drupal::entityManager()->getFieldMapByFieldType('image'); foreach ($fields as $field) { foreach ($field as $key => $value) { $field_options[$key] = $key; } } $form['masonry_background'] = array('#type' => 'select', '#title' => t('Image'), '#options' => $field_options, '#default_value' => $this->options['masonry_background'], '#states' => array('visible' => array('.grid-style' => array('value' => 'masonry_resize')))); $form['grid_ratio'] = array('#type' => 'textfield', '#title' => t('Ratio'), '#description' => t('The ratio image'), '#default_value' => $this->options['grid_ratio'], '#states' => array('visible' => array('.grid-style' => array('value' => 'masonry_resize')))); $form['grid_cols_lg'] = array('#type' => 'select', '#title' => t('Large Desktop Items'), '#description' => t('Number of items on large desktop'), '#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12), '#default_value' => $this->options['grid_cols_lg']); $form['grid_cols_md'] = array('#type' => 'select', '#title' => t('Desktop Items'), '#description' => t('Number of items on desktop'), '#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12), '#default_value' => $this->options['grid_cols_md']); $form['grid_cols_sm'] = array('#type' => 'select', '#title' => t('Tablet Items'), '#description' => t('Number of items on tablet'), '#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12), '#default_value' => $this->options['grid_cols_sm']); $form['grid_cols_xs'] = array('#type' => 'select', '#title' => t('Phone Items'), '#description' => t('Number of items on phone'), '#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12), '#default_value' => $this->options['grid_cols_xs']); $form['grid_margin'] = array('#type' => 'textfield', '#title' => t('Margin'), '#description' => t('The spacing beetween items'), '#default_value' => $this->options['grid_margin'], '#field_suffix' => 'px'); $form['grid_filter'] = array('#type' => 'select', '#title' => t('Use Filter'), '#options' => array(0 => t('No'), 1 => t('Yes')), '#description' => t('Filter items by taxonomy term'), '#default_value' => $this->options['grid_filter'], '#attributes' => array('class' => array('grid-filter-option'))); $categories = array(); $categories['select'] = t('Select'); foreach (Vocabulary::loadMultiple() as $vocabulary) { $categories[$vocabulary->id()] = $vocabulary->get('name'); } $form['grid_filter_vocabulary'] = array('#type' => 'select', '#title' => t('Filter Vocabulary'), '#options' => $categories, '#description' => t('Which taxonomy vocabulary do you want to use for the filter'), '#default_value' => $this->options['grid_filter_vocabulary'], '#states' => array('visible' => array('.grid-filter-option' => array('value' => 1)))); }
/** * Test the vocabulary overview with no vocabularies. */ function testTaxonomyAdminNoVocabularies() { // Delete all vocabularies. $vocabularies = Vocabulary::loadMultiple(); foreach ($vocabularies as $key => $vocabulary) { $vocabulary->delete(); } // Confirm that no vocabularies are found in the database. $this->assertFalse(Vocabulary::loadMultiple(), 'No vocabularies found.'); $this->drupalGet('admin/structure/taxonomy'); // Check the default message for no vocabularies. $this->assertText(t('No vocabularies available.')); }
/** * Tests for loading multiple vocabularies. */ function testTaxonomyVocabularyLoadMultiple() { // Delete any existing vocabularies. foreach (Vocabulary::loadMultiple() as $vocabulary) { $vocabulary->delete(); } // Create some vocabularies and assign weights. $vocabulary1 = $this->createVocabulary(); $vocabulary1->set('weight', 0); $vocabulary1->save(); $vocabulary2 = $this->createVocabulary(); $vocabulary2->set('weight', 1); $vocabulary2->save(); $vocabulary3 = $this->createVocabulary(); $vocabulary3->set('weight', 2); $vocabulary3->save(); // Check if third party settings exist. $this->assertEqual('bar', $vocabulary1->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.'); $this->assertEqual('bar', $vocabulary2->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.'); $this->assertEqual('bar', $vocabulary3->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.'); // Fetch the names for all vocabularies, confirm that they are keyed by // machine name. $names = taxonomy_vocabulary_get_names(); $this->assertEqual($names[$vocabulary1->id()], $vocabulary1->id(), 'Vocabulary 1 name found.'); // Fetch the vocabularies with entity_load_multiple(), specifying IDs. // Ensure they are returned in the same order as the original array. $vocabularies = Vocabulary::loadMultiple(array($vocabulary3->id(), $vocabulary2->id(), $vocabulary1->id())); $loaded_order = array_keys($vocabularies); $expected_order = array($vocabulary3->id(), $vocabulary2->id(), $vocabulary1->id()); $this->assertIdentical($loaded_order, $expected_order); // Test loading vocabularies by their properties. $controller = $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary'); // Fetch vocabulary 1 by name. $vocabulary = current($controller->loadByProperties(array('name' => $vocabulary1->label()))); $this->assertEqual($vocabulary->id(), $vocabulary1->id(), 'Vocabulary loaded successfully by name.'); // Fetch vocabulary 2 by name and ID. $vocabulary = current($controller->loadByProperties(array('name' => $vocabulary2->label(), 'vid' => $vocabulary2->id()))); $this->assertEqual($vocabulary->id(), $vocabulary2->id(), 'Vocabulary loaded successfully by name and ID.'); }
/** * Function to display the faq page. * * @param int $tid * Default is 0, determines if the questions and answers on the page * will be shown according to a category or non-categorized. * @param string $faq_display * Optional parameter to override default question layout setting. * @param string $category_display * Optional parameter to override default category layout setting. * @return * The page with FAQ questions and answers. * @throws NotFoundHttpException */ public function faqPage($tid = 0, $faq_display = '', $category_display = '') { $faq_settings = \Drupal::config('faq.settings'); $output = $output_answers = ''; $build = array(); $build['#type'] = 'markup'; $build['#attached']['css'] = array(drupal_get_path('module', 'faq') . '/css/faq.css'); $build['#title'] = $faq_settings->get('title'); if (!$this->moduleHandler()->moduleExists('taxonomy')) { $tid = 0; } $faq_display = $faq_settings->get('display'); $use_categories = $faq_settings->get('use_categories'); $category_display = $faq_settings->get('category_display'); // if taxonomy doesn't installed, do not use categories if (!$this->moduleHandler()->moduleExists('taxonomy')) { $use_categories = FALSE; } if ($use_categories && $category_display == 'hide_qa' || $faq_display == 'hide_answer') { $build['#attached']['js'] = array(array('data' => drupal_get_path('module', 'faq') . '/js/faq.js'), array('data' => array('hide_qa_accordion' => $faq_settings->get('hide_qa_accordion'), 'category_hide_qa_accordion' => $faq_settings->get('category_hide_qa_accordion')), 'type' => 'setting')); } // Non-categorized questions and answers. if (!$use_categories || $category_display == 'none' && empty($tid)) { if (!empty($tid)) { throw new NotFoundHttpException(); } $default_sorting = $faq_settings->get('default_sorting'); $query = db_select('node', 'n'); $weight_alias = $query->leftJoin('faq_weights', 'w', '%alias.nid=n.nid'); $node_data = $query->leftJoin('node_field_data', 'd', 'd.nid=n.nid'); $query->addTag('node_access')->fields('n', array('nid'))->condition('n.type', 'faq')->condition('d.status', 1)->condition(db_or()->condition("{$weight_alias}.tid", 0)->isNull("{$weight_alias}.tid")); $default_weight = 0; if ($default_sorting == 'ASC') { $default_weight = 1000000; } // Works, but involves variable concatenation - safe though, since // $default_weight is an integer. $query->addExpression("COALESCE(w.weight, {$default_weight})", 'effective_weight'); // Doesn't work in Postgres. //$query->addExpression('COALESCE(w.weight, CAST(:default_weight as SIGNED))', 'effective_weight', array(':default_weight' => $default_weight)); $query->orderBy('effective_weight', 'ASC')->orderBy('d.sticky', 'DESC'); if ($default_sorting == 'ASC') { $query->orderBy('d.created', 'ASC'); } else { $query->orderBy('d.created', 'DESC'); } // Only need the nid column. $nids = $query->execute()->fetchCol(); $data = Node::loadMultiple($nids); $questions_to_render = array(); $questions_to_render['#data'] = $data; switch ($faq_display) { case 'questions_top': $questions_to_render['#theme'] = 'faq_questions_top'; break; case 'hide_answer': $questions_to_render['#theme'] = 'faq_hide_answer'; break; case 'questions_inline': $questions_to_render['#theme'] = 'faq_questions_inline'; break; case 'new_page': $questions_to_render['#theme'] = 'faq_new_page'; break; } // End of switch. $output = drupal_render($questions_to_render); } else { $hide_child_terms = $faq_settings->get('hide_child_terms'); // If we're viewing a specific category/term. if (!empty($tid)) { if ($term = Term::load($tid)) { $title = $faq_settings->get('title'); $build['#title'] = $title . ($title ? ' - ' : '') . $this->t($term->getName()); $this->_displayFaqByCategory($faq_display, $category_display, $term, 0, $output, $output_answers); $to_render = array('#theme' => 'faq_page', '#content' => SafeMarkup::set($output), '#answers' => SafeMarkup::set($output_answers)); $build['#markup'] = drupal_render($to_render); return $build; } else { throw new NotFoundHttpException(); } } $list_style = $faq_settings->get('category_listing'); $vocabularies = Vocabulary::loadMultiple(); $vocab_omit = $faq_settings->get('omit_vocabulary'); $items = array(); $vocab_items = array(); foreach ($vocabularies as $vid => $vobj) { if (isset($vocab_omit[$vid]) && $vocab_omit[$vid] != 0) { continue; } if ($category_display == "new_page") { $vocab_items = $this->_getIndentedFaqTerms($vid, 0); $items = array_merge($items, $vocab_items); } else { if ($hide_child_terms && $category_display == 'hide_qa') { $tree = taxonomy_get_tree($vid, 0, 1, TRUE); } else { $tree = taxonomy_get_tree($vid, 0, NULL, TRUE); } foreach ($tree as $term) { switch ($category_display) { case 'hide_qa': case 'categories_inline': if (FaqHelper::taxonomyTermCountNodes($term->id())) { $this->_displayFaqByCategory($faq_display, $category_display, $term, 1, $output, $output_answers); } break; } } } } if ($category_display == "new_page") { $output = $this->_renderCategoriesToList($items, $list_style); } } $faq_description = $faq_settings->get('description'); $markup = array('#theme' => 'faq_page', '#content' => SafeMarkup::set($output), '#answers' => SafeMarkup::set($output_answers), '#description' => SafeMarkup::set($faq_description)); $build['#markup'] = drupal_render($markup); return $build; }
/** * {@inheritdoc} */ public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) { $vocabularies = Vocabulary::loadMultiple(); $options = array(); foreach ($vocabularies as $vocabulary) { $options[$vocabulary->id()] = $vocabulary->label(); } $element = array(); $element['#tree'] = TRUE; foreach ($this->getSetting('allowed_values') as $delta => $tree) { $element['allowed_values'][$delta]['vocabulary'] = array('#type' => 'select', '#title' => t('Vocabulary'), '#default_value' => $tree['vocabulary'], '#options' => $options, '#required' => TRUE, '#description' => t('The vocabulary which supplies the options for this field.'), '#disabled' => $has_data); $element['allowed_values'][$delta]['parent'] = array('#type' => 'value', '#value' => $tree['parent']); } return $element; }
/** * Returns an array containing the vocabularies related to the FAQ node type. * * @return array Array containing the FAQ related vocabularies. */ public static function faqRelatedVocabularies() { $vids = array(); foreach (\Drupal::entityManager()->getFieldDefinitions('node', 'faq') as $field_definition) { if ($field_definition->getType() == 'taxonomy_term_reference') { foreach ($field_definition->getSetting('allowed_values') as $allowed_values) { $vids[] = $allowed_values['vocabulary']; } } } return Vocabulary::loadMultiple($vids); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $category = NULL) { //get category id from route values if (is_numeric(FaqHelper::arg(1))) { $category = FaqHelper::arg(1); } $order = $date_order = ''; $faq_settings = $this->config('faq.settings'); $use_categories = $faq_settings->get('use_categories'); if (!$use_categories) { $step = "order"; } elseif (!isset($form_state['values']) && empty($category)) { $step = "categories"; } else { $step = "order"; } $form['step'] = array('#type' => 'value', '#value' => $step); // Categorized q/a. if ($step == "categories") { // Get list of categories. $vocabularies = Vocabulary::loadMultiple(); $options = array(); foreach ($vocabularies as $vid => $vobj) { $tree = taxonomy_get_tree($vid); foreach ($tree as $term) { if (!FaqHelper::taxonomyTermCountNodes($term->tid)) { continue; } $options[$term->tid] = $this->t($term->name); $form['choose_cat']['faq_category'] = array('#type' => 'select', '#title' => t('Choose a category'), '#description' => t('Choose a category that you wish to order the questions for.'), '#options' => $options, '#multiple' => FALSE); $form['choose_cat']['search'] = array('#type' => 'submit', '#value' => t('Search'), '#submit' => array('faq_order_settings_choose_cat_form_submit')); } } } else { $default_sorting = $faq_settings->get('default_sorting'); $default_weight = 0; if ($default_sorting != 'DESC') { $default_weight = 1000000; } $options = array(); if (!empty($form_state['values']['faq_category'])) { $category = $form_state['values']['faq_category']; } // Uncategorized ordering. $query = db_select('node', 'n'); $query->join('node_field_data', 'd', 'n.nid = d.nid'); $query->fields('n', array('nid'))->fields('d', array('title'))->addTag('node_access')->condition('n.type', 'faq')->condition('d.status', 1); // Works, but involves variable concatenation - safe though, since // $default_weight is an integer. $query->addExpression("COALESCE(w.weight, {$default_weight})", 'effective_weight'); // Doesn't work in Postgres. //$query->addExpression('COALESCE(w.weight, CAST(:default_weight as SIGNED))', 'effective_weight', array(':default_weight' => $default_weight)); if (empty($category)) { $category = 0; $w_alias = $query->leftJoin('faq_weights', 'w', 'n.nid = %alias.nid AND %alias.tid = :category', array(':category' => $category)); $query->orderBy('effective_weight', 'ASC')->orderBy('d.sticky', 'DESC')->orderBy('d.created', $default_sorting == 'DESC' ? 'DESC' : 'ASC'); } else { $ti_alias = $query->innerJoin('taxonomy_index', 'ti', '(n.nid = %alias.nid)'); $w_alias = $query->leftJoin('faq_weights', 'w', 'n.nid = %alias.nid AND %alias.tid = :category', array(':category' => $category)); $query->condition('ti.tid', $category); $query->orderBy('effective_weight', 'ASC')->orderBy('d.sticky', 'DESC')->orderBy('d.created', $default_sorting == 'DESC' ? 'DESC' : 'ASC'); } $options = $query->execute()->fetchAll(); $form['weight']['faq_category'] = array('#type' => 'value', '#value' => $category); // Show table ordering form. $form['order_no_cats']['#tree'] = TRUE; $form['order_no_cats']['#theme'] = 'faq_draggable_question_order_table'; foreach ($options as $i => $record) { $form['order_no_cats'][$i]['nid'] = array('#type' => 'hidden', '#value' => $record->nid); $form['order_no_cats'][$i]['title'] = array('#markup' => String::checkPlain($record->title)); $form['order_no_cats'][$i]['sort'] = array('#type' => 'weight', '#delta' => count($options), '#default_value' => $i); } $form['actions']['#type'] = 'actions'; $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save order'), '#button_type' => 'primary'); } return $form; }