"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.9/core/modules/views/src/Plugin/views/argument/ManyToOne.php" (18 Nov 2020, 5852 Bytes) of package /linux/www/drupal-8.9.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ManyToOne.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\views\Plugin\views\argument;
    4 
    5 use Drupal\Core\Form\FormStateInterface;
    6 use Drupal\views\ViewExecutable;
    7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
    8 use Drupal\views\ManyToOneHelper;
    9 
   10 /**
   11  * An argument handler for use in fields that have a many to one relationship
   12  * with the table(s) to the left. This adds a bunch of options that are
   13  * reasonably common with this type of relationship.
   14  * Definition terms:
   15  * - numeric: If true, the field will be considered numeric. Probably should
   16  *   always be set TRUE as views_handler_argument_string has many to one
   17  *   capabilities.
   18  * - zero is null: If true, a 0 will be handled as empty, so for example
   19  *   a default argument can be provided or a summary can be shown.
   20  *
   21  * @ingroup views_argument_handlers
   22  *
   23  * @ViewsArgument("many_to_one")
   24  */
   25 class ManyToOne extends ArgumentPluginBase {
   26 
   27   /**
   28    * {@inheritdoc}
   29    */
   30   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
   31     parent::init($view, $display, $options);
   32 
   33     $this->helper = new ManyToOneHelper($this);
   34 
   35     // Ensure defaults for these, during summaries and stuff:
   36     $this->operator = 'or';
   37     $this->value = [];
   38   }
   39 
   40   protected function defineOptions() {
   41     $options = parent::defineOptions();
   42 
   43     if (!empty($this->definition['numeric'])) {
   44       $options['break_phrase'] = ['default' => FALSE];
   45     }
   46 
   47     $options['add_table'] = ['default' => FALSE];
   48     $options['require_value'] = ['default' => FALSE];
   49 
   50     if (isset($this->helper)) {
   51       $this->helper->defineOptions($options);
   52     }
   53     else {
   54       $helper = new ManyToOneHelper($this);
   55       $helper->defineOptions($options);
   56     }
   57 
   58     return $options;
   59   }
   60 
   61   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
   62     parent::buildOptionsForm($form, $form_state);
   63 
   64     // allow + for or, , for and
   65     $form['break_phrase'] = [
   66       '#type' => 'checkbox',
   67       '#title' => $this->t('Allow multiple values'),
   68       '#description' => $this->t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
   69       '#default_value' => !empty($this->options['break_phrase']),
   70       '#group' => 'options][more',
   71     ];
   72 
   73     $form['add_table'] = [
   74       '#type' => 'checkbox',
   75       '#title' => $this->t('Allow multiple filter values to work together'),
   76       '#description' => $this->t('If selected, multiple instances of this filter can work together, as though multiple values were supplied to the same filter. This setting is not compatible with the "Reduce duplicates" setting.'),
   77       '#default_value' => !empty($this->options['add_table']),
   78       '#group' => 'options][more',
   79     ];
   80 
   81     $form['require_value'] = [
   82       '#type' => 'checkbox',
   83       '#title' => $this->t('Do not display items with no value in summary'),
   84       '#default_value' => !empty($this->options['require_value']),
   85       '#group' => 'options][more',
   86     ];
   87 
   88     $this->helper->buildOptionsForm($form, $form_state);
   89   }
   90 
   91   /**
   92    * Override ensureMyTable so we can control how this joins in.
   93    * The operator actually has influence over joining.
   94    */
   95   public function ensureMyTable() {
   96     $this->helper->ensureMyTable();
   97   }
   98 
   99   public function query($group_by = FALSE) {
  100     $empty = FALSE;
  101     if (isset($this->definition['zero is null']) && $this->definition['zero is null']) {
  102       if (empty($this->argument)) {
  103         $empty = TRUE;
  104       }
  105     }
  106     else {
  107       if (!isset($this->argument)) {
  108         $empty = TRUE;
  109       }
  110     }
  111     if ($empty) {
  112       parent::ensureMyTable();
  113       $this->query->addWhere(0, "$this->tableAlias.$this->realField", NULL, 'IS NULL');
  114       return;
  115     }
  116 
  117     if (!empty($this->options['break_phrase'])) {
  118       $force_int = !empty($this->definition['numeric']);
  119       $this->unpackArgumentValue($force_int);
  120     }
  121     else {
  122       $this->value = [$this->argument];
  123       $this->operator = 'or';
  124     }
  125 
  126     $this->helper->addFilter();
  127   }
  128 
  129   public function title() {
  130     if (!$this->argument) {
  131       return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : $this->t('Uncategorized');
  132     }
  133 
  134     if (!empty($this->options['break_phrase'])) {
  135       $force_int = !empty($this->definition['numeric']);
  136       $this->unpackArgumentValue($force_int);
  137     }
  138     else {
  139       $this->value = [$this->argument];
  140       $this->operator = 'or';
  141     }
  142 
  143     // @todo -- both of these should check definition for alternate keywords.
  144 
  145     if (empty($this->value)) {
  146       return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : $this->t('Uncategorized');
  147     }
  148 
  149     if ($this->value === [-1]) {
  150       return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : $this->t('Invalid input');
  151     }
  152 
  153     return implode($this->operator == 'or' ? ' + ' : ', ', $this->titleQuery());
  154   }
  155 
  156   protected function summaryQuery() {
  157     $field = $this->table . '.' . $this->field;
  158     $join = $this->getJoin();
  159 
  160     if (!empty($this->options['require_value'])) {
  161       $join->type = 'INNER';
  162     }
  163 
  164     if (empty($this->options['add_table']) || empty($this->view->many_to_one_tables[$field])) {
  165       $this->tableAlias = $this->query->ensureTable($this->table, $this->relationship, $join);
  166     }
  167     else {
  168       $this->tableAlias = $this->helper->summaryJoin();
  169     }
  170 
  171     // Add the field.
  172     $this->base_alias = $this->query->addField($this->tableAlias, $this->realField);
  173 
  174     $this->summaryNameField();
  175 
  176     return $this->summaryBasics();
  177   }
  178 
  179   public function summaryArgument($data) {
  180     $value = $data->{$this->base_alias};
  181     if (empty($value)) {
  182       $value = 0;
  183     }
  184 
  185     return $value;
  186   }
  187 
  188   /**
  189    * Override for specific title lookups.
  190    */
  191   public function titleQuery() {
  192     return $this->value;
  193   }
  194 
  195 }