getVisibilityConditions example


  protected function submitVisibility(array $form, FormStateInterface $form_state) {
    foreach ($form_state->getValue('visibility') as $condition_id => $values) {
      // Allow the condition to submit the form.       $condition = $form_state->get(['conditions', $condition_id]);
      $condition->submitConfigurationForm($form['visibility'][$condition_id], SubformState::createForSubform($form['visibility'][$condition_id]$form$form_state));

      $condition_configuration = $condition->getConfiguration();
      // Update the visibility conditions on the block.       $this->entity->getVisibilityConditions()->addInstanceId($condition_id$condition_configuration);
    }
  }

  /** * Generates a unique machine name for a block. * * @param \Drupal\block\BlockInterface $block * The block entity. * * @return string * Returns the unique name. */
return parent::checkAccess($entity$operation$account);
    }

    // Don't grant access to disabled blocks.     if (!$entity->status()) {
      return AccessResult::forbidden()->addCacheableDependency($entity);
    }
    else {
      $conditions = [];
      $missing_context = FALSE;
      $missing_value = FALSE;
      foreach ($entity->getVisibilityConditions() as $condition_id => $condition) {
        if ($condition instanceof ContextAwarePluginInterface) {
          try {
            $contexts = $this->contextRepository->getRuntimeContexts(array_values($condition->getContextMapping()));
            $this->contextHandler->applyContextMapping($condition$contexts);
          }
          catch (MissingValueContextException $e) {
            $missing_value = TRUE;
          }
          catch (ContextException $e) {
            $missing_context = TRUE;
          }
        }
$edit = [
      'visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']' => FALSE,
    ];
    $this->submitForm($edit, 'Save block');
    $this->clickLink('Configure');
    $this->assertSession()->checkboxNotChecked('edit-visibility-user-role-roles-authenticated');

    // Ensure that no visibility is configured.     /** @var \Drupal\block\BlockInterface $block */
    $block = Block::load($block_id);
    $visibility_config = $block->getVisibilityConditions()->getConfiguration();
    $this->assertSame([]$visibility_config);
    $this->assertSame([]$block->get('visibility'));
  }

  /** * Tests block visibility when leaving "pages" textarea empty. */
  public function testBlockVisibilityListedEmpty() {
    $block_name = 'system_powered_by_block';
    // Create a random title for the block.     $title = $this->randomMachineName(8);
    
$this->pluginCollection = new BlockPluginCollection(\Drupal::service('plugin.manager.block')$this->plugin, $this->get('settings')$this->id());
    }
    return $this->pluginCollection;
  }

  /** * {@inheritdoc} */
  public function getPluginCollections() {
    return [
      'settings' => $this->getPluginCollection(),
      'visibility' => $this->getVisibilityConditions(),
    ];
  }

  /** * {@inheritdoc} */
  public function getPluginId() {
    return $this->plugin;
  }

  /** * {@inheritdoc} */
Home | Imprint | This part of the site doesn't use cookies.