You are here

class MenuChildrenNodeJoin in Views Menu Node Children Filter 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/views/join/MenuChildrenNodeJoin.php \Drupal\views_menu_children_filter\Plugin\views\join\MenuChildrenNodeJoin
  2. 3.0.x src/Plugin/views/join/MenuChildrenNodeJoin.php \Drupal\views_menu_children_filter\Plugin\views\join\MenuChildrenNodeJoin

Views Join plugin to join the Node table to the menu_tree table.

@package Drupal\views_menu_children_filter

Plugin annotation

@ViewsJoin("menu_children_node_join");

Hierarchy

Expanded class hierarchy of MenuChildrenNodeJoin

2 files declare their use of MenuChildrenNodeJoin
MenuChildren.php in src/Plugin/views/sort/MenuChildren.php
MenuChildren.php in src/Plugin/views/argument/MenuChildren.php
1 string reference to 'MenuChildrenNodeJoin'
views_menu_children_filter.services.yml in ./views_menu_children_filter.services.yml
views_menu_children_filter.services.yml
1 service uses MenuChildrenNodeJoin
views_menu_children_filter.join_handler in ./views_menu_children_filter.services.yml
Drupal\views_menu_children_filter\Plugin\views\join\MenuChildrenNodeJoin

File

src/Plugin/views/join/MenuChildrenNodeJoin.php, line 18

Namespace

Drupal\views_menu_children_filter\Plugin\views\join
View source
class MenuChildrenNodeJoin extends JoinPluginBase {

  /**
   * The values to concat with node.nid in to join on the menu_tree's route_param_key column.
   *
   * @var array Defaults to: [ 'entity:node/' ].
   */
  public $prefixes = array(
    'entity:node/',
  );

  /**
   * MenuChildrenNodeJoin constructor.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler Required to setup hook_TYPE_alter hooks so module's can alter this join.
   * @param array $configuration
   * @param string $plugin_id
   * @param mixed $plugin_definition
   */
  public function __construct(ModuleHandlerInterface $module_handler, array $configuration, $plugin_id, $plugin_definition) {

    // Define a hook_TYPE_alter hook for other modules to change the "prefixes" used in the SQL join.
    $module_handler
      ->alter('menu_children_filter_join_prefix', $this->prefixes);
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * Creates an instance of the plugin.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The container to pull out services used in the plugin.
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   *
   * @return static
   *   Returns an instance of this plugin.
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {

    // Setup some defaults.
    $configuration = array_merge(array(
      'type' => 'INNER',
      'table' => 'menu_link_content_data',
      'field' => false,
      // Formula value handled in the "buildJoin" function.
      'left_table' => false,
      'left_field' => false,
      'operator' => '=',
    ), $configuration);

    // TODO: Remove this after dev work completed.
    // $test = $container->get('views_menu_children_filter.join_handler');
    $plugin_id = empty($plugin_id) ? "menu_children_node_join" : $plugin_id;
    return new static($container
      ->get('module_handler'), $configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  function buildJoin($select_query, $table, $view_query) {
    $values = array();
    $node_table_alias = $select_query
      ->getTables()['node_field_data']['alias'];
    $condition_parts = [];
    foreach ($this->prefixes as $prefix) {

      // Concatenate prefix with node.nid and provide as a parametrized value.
      $placeholder = ':views_join_condition_' . $select_query
        ->nextPlaceholder();
      $values[$placeholder] = $prefix;
      $condition_parts[] = "( CONCAT({$placeholder}, {$node_table_alias}.nid) = {$this->table}.link__uri)";
    }
    $condition = sprintf('(%s.enabled = 1) AND (%s)', $this->table, implode(' OR ', $condition_parts));
    $select_query
      ->addJoin($this->type, $this->table, $table['alias'], $condition, $values);
  }

  /**
   * @param Sql $query The query that the join will be added to.
   * @param bool $allow_duplicate_join If "false", prevents this join from joining more than once if this function is called repeatedly.
   */
  public function joinToNodeTable(Sql $query, $allow_duplicate_join = false) {

    // Because this can be called from the argument and sort handlers,
    // first check to see if the join as already been applied.
    if (!$allow_duplicate_join && isset($query->tables['node_field_data']['menu_link_content_data'])) {
      return;
    }
    $query
      ->queueTable("menu_link_content_data", "node_field_data", $this);
  }

  /**
   * Filter the query by either a: parent node, page page via its link_path, or null and limit to root nodes.
   *
   * @param \Drupal\views\Plugin\views\query\Sql $query The $query The query we're going to alter.
   * @param \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent $link The by parent link.
   */
  public static function filterByPage(Sql $query, $link) {
    $parent = $link
      ->getPluginId();
    $query
      ->addWhereExpression(0, 'menu_link_content_data.parent = :parent_lid', array(
      ':parent_lid' => $parent,
    ));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
JoinPluginBase::$adjusted public property Defines whether a join has been adjusted.
JoinPluginBase::$configuration public property The configuration array passed by initJoin. Overrides PluginBase::$configuration
JoinPluginBase::$extra public property An array of extra conditions on the join.
JoinPluginBase::$extraOperator public property How all the extras will be combined. Either AND or OR.
JoinPluginBase::$field public property The field to join on (right field).
JoinPluginBase::$leftField public property The field we join to.
JoinPluginBase::$leftFormula public property A formula to be used instead of the left field.
JoinPluginBase::$leftTable public property The table we join to.
JoinPluginBase::$table public property The table to join (right table).
JoinPluginBase::$type public property The join type, so for example LEFT (default) or INNER.
JoinPluginBase::buildExtra protected function Builds a single extra condition.
JoinPluginBase::joinAddExtra protected function Adds the extras to the join condition. 1
MenuChildrenNodeJoin::$prefixes public property The values to concat with node.nid in to join on the menu_tree's route_param_key column.
MenuChildrenNodeJoin::buildJoin function Builds the SQL for the join this object represents. Overrides JoinPluginBase::buildJoin
MenuChildrenNodeJoin::create public static function Creates an instance of the plugin.
MenuChildrenNodeJoin::filterByPage public static function Filter the query by either a: parent node, page page via its link_path, or null and limit to root nodes.
MenuChildrenNodeJoin::joinToNodeTable public function
MenuChildrenNodeJoin::__construct public function MenuChildrenNodeJoin constructor. Overrides JoinPluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.