現在のViewsクエリを保存・再現する方法

カテゴリ Views コアバージョン 8.4x 関連モジュール hook_views_query_alter

modules/custom/your_module/your_module.module

/**
 * @param \Drupal\views\ViewExecutable $view
 * @param \Drupal\views\Plugin\views\query\QueryPluginBase $query
 */
function your_module_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($view->id() == 'your_view_id') {
    // クエリ保存
    $q = $query->query();
    $保存場所 = [
      'string' => $q->__toString(),
      'arguments' => $q->arguments(),
    ];
  }
}

再現

$result = \Drupal::database()
  ->query($保存場所['string'], $保存場所['arguments']);

OTHER FAQ