/** * Presents a list of conditions to add to the block_visibility_group entity. * * @param \Drupal\block_visibility_groups\Entity\BlockVisibilityGroup $block_visibility_group * The block_visibility_group entity. * * @return array * The condition selection page. */ public function selectCondition(BlockVisibilityGroup $block_visibility_group, $redirect) { $build = [ '#theme' => 'links', '#links' => [], ]; $available_plugins = $this->conditionManager->getDefinitions(); // @todo Should nesting Conditions be allowed unset($available_plugins['condition_group']); foreach ($available_plugins as $condition_id => $condition) { $build['#links'][$condition_id] = [ 'title' => $condition['label'], 'url' => Url::fromRoute('block_visibility_groups.condition_add', [ 'block_visibility_group' => $block_visibility_group->id(), 'condition_id' => $condition_id, 'redirect' => $redirect, ]), 'attributes' => [ 'class' => ['use-ajax'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode([ 'width' => 'auto', ]), ], ]; } return $build; }
/** * {@inheritdoc} */ public function listBlocks(Request $request, $theme, BlockVisibilityGroup $block_visibility_group = NULL) { $list = parent::listBlocks($request, $theme); if ($block_visibility_group) { foreach ($list['blocks']['#rows'] as &$row) { $row['operations']['data']['#links']['add']['query']['block_visibility_group'] = $block_visibility_group->id(); } } return $list; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Allow the condition to submit the form. $condition_values = (new FormState())->setValues($form_state->getValue('condition')); $this->condition->submitConfigurationForm($form, $condition_values); // Update the original form values. $form_state->setValue('condition', $condition_values->getValues()); if ($this->condition instanceof ContextAwarePluginInterface) { $this->condition->setContextMapping($condition_values->getValue('context_mapping', [])); } // Set the submission message. drupal_set_message($this->submitMessageText()); $configuration = $this->condition->getConfiguration(); // If this condition is new, add it to the block_visibility_group. if (!isset($configuration['uuid'])) { $this->block_visibility_group->addCondition($configuration); } // Save the block_visibility_group entity. $this->block_visibility_group->save(); $this->setConditionRedirect($form_state); }
/** * @param \Drupal\block_visibility_groups\Entity\BlockVisibilityGroup $block_visibility_group * * @return boolean */ public function evaluateGroup(BlockVisibilityGroup $block_visibility_group) { $group_id = $block_visibility_group->id(); if (!isset($this->group_evaluations[$group_id])) { /** @var ConditionPluginCollection $conditions */ $conditions = $block_visibility_group->getConditions(); if (empty($conditions)) { // If no conditions then always true. return TRUE; } $logic = $block_visibility_group->getLogic(); if ($this->applyContexts($conditions, $logic)) { $this->group_evaluations[$group_id] = $this->resolveConditions($conditions, $logic); } else { $this->group_evaluations[$group_id] = FALSE; } } return $this->group_evaluations[$group_id]; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->block_visibility_group->removeCondition($this->condition->getConfiguration()['uuid']); $this->block_visibility_group->save(); drupal_set_message($this->t('The condition %name has been removed.', ['%name' => $this->condition->getPluginDefinition()['label']])); $this->setConditionRedirect($form_state); }
/** * @param $config * * @return static */ private function createGroup($configs) { $group = BlockVisibilityGroup::create( [ 'id' => $this->randomMachineName(), 'label' => $this->randomString(), ] ); $group->save(); foreach ($configs as $config) { $group->addCondition($config); } $group->save(); return $group; }
/** * @param array $form * @param $group * * @return array */ protected function createHelp(BlockVisibilityGroup $group) { $help = '<strong>' . $this->t('Currently viewing') . ': <em>' . $group->label() . '</em></strong>'; if ($group->getLogic() == 'and') { $help .= '<p>' . $this->t('All conditions must pass.') . '</p>'; } else { $help .= '<p>' . $this->t('Only one condition must pass.') . '</p>'; } if ($group->isAllowOtherConditions()) { $help .= '<p>' . $this->t('Blocks in this group may have other conditions.') . '</p>'; } else { $help .= '<p>' . $this->t('Blocks in this group may <strong>not</strong> have other conditions.') . '</p>'; } $help_group = [ '#type' => 'details', '#open' => FALSE, '#title' => $this->t('Group Settings'), 'text' => [ '#type' => 'markup', '#markup' => $help, ], 'edit' => [ '#type' => 'link', '#title' => t('Edit Group Settings'), '#url' => $group->urlInfo('edit-form'), ], ]; return $help_group; }
/** * @param $id * @param $label * @param $configs * * @return \Drupal\block_visibility_groups\Entity\BlockVisibilityGroup */ protected function createGroup($id, $label, $configs) { /** @var BlockVisibilityGroup $group */ $group = BlockVisibilityGroup::create( [ 'id' => $id, 'label' => $label, ] ); $group->save(); foreach ($configs as $config) { $group->addCondition($config); } $group->save(); return $group; }
/** * @param array $form * @param $block_visibility_group * * @return array */ protected function createConditionsSet(array $form, BlockVisibilityGroup $block_visibility_group, $redirect = 'edit') { $attributes = [ 'class' => ['use-ajax'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode([ 'width' => 'auto', ]), ]; $add_button_attributes = NestedArray::mergeDeep($attributes, [ 'class' => [ 'button', 'button--small', 'button-action', 'form-item', ], ]); $form['conditions_section'] = [ '#type' => 'fieldset', '#title' => $this->t('Conditions'), '#open' => TRUE, ]; $form['conditions_section']['add_condition'] = [ '#type' => 'link', '#title' => $this->t('Add new condition'), '#url' => Url::fromRoute('block_visibility_groups.condition_select', [ 'block_visibility_group' => $block_visibility_group->id(), 'redirect' => $redirect, ]), '#attributes' => $add_button_attributes, '#attached' => [ 'library' => [ 'core/drupal.ajax', ], ], ]; if ($conditions = $block_visibility_group->getConditions()) { $form['conditions_section']['conditions'] = [ '#type' => 'table', '#header' => [ $this->t('Label'), $this->t('Description'), $this->t('Operations'), ], '#empty' => $this->t('There are no conditions.'), ]; foreach ($conditions as $condition_id => $condition) { $row = []; $row['label']['#markup'] = $condition->getPluginDefinition()['label']; $row['description']['#markup'] = $condition->summary(); $operations = []; $operations['edit'] = [ 'title' => $this->t('Edit'), 'url' => Url::fromRoute('block_visibility_groups.condition_edit', [ 'block_visibility_group' => $block_visibility_group->id(), 'condition_id' => $condition_id, 'redirect' => $redirect, ]), 'attributes' => $attributes, ]; $operations['delete'] = [ 'title' => $this->t('Delete'), 'url' => Url::fromRoute('block_visibility_groups.condition_delete', [ 'block_visibility_group' => $block_visibility_group->id(), 'condition_id' => $condition_id, 'redirect' => $redirect, ]), 'attributes' => $attributes, ]; $row['operations'] = [ '#type' => 'operations', '#links' => $operations, ]; $form['conditions_section']['conditions'][$condition_id] = $row; } } return $form['conditions_section']; }