Chris@0: pluginId = $plugin_id; Chris@0: $delta = $this->getDerivativeId(); Chris@0: list($name, $this->displayID) = explode('-', $delta, 2); Chris@0: // Load the view. Chris@0: $view = $storage->load($name); Chris@0: $this->view = $executable_factory->get($view); Chris@0: $this->displaySet = $this->view->setDisplay($this->displayID); Chris@0: $this->user = $user; Chris@0: Chris@0: parent::__construct($configuration, $plugin_id, $plugin_definition); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { Chris@0: return new static( Chris@0: $configuration, $plugin_id, $plugin_definition, Chris@0: $container->get('views.executable'), Chris@0: $container->get('entity.manager')->getStorage('view'), Chris@0: $container->get('current_user') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function blockAccess(AccountInterface $account) { Chris@0: if ($this->view->access($this->displayID)) { Chris@0: $access = AccessResult::allowed(); Chris@0: } Chris@0: else { Chris@0: $access = AccessResult::forbidden(); Chris@0: } Chris@0: return $access; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function defaultConfiguration() { Chris@0: return ['views_label' => '']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: public function getPreviewFallbackString() { Chris@18: return $this->t('"@view" views block', ['@view' => $this->view->storage->label()]); Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@0: public function buildConfigurationForm(array $form, FormStateInterface $form_state) { Chris@0: $form = parent::buildConfigurationForm($form, $form_state); Chris@0: Chris@0: // Set the default label to '' so the views internal title is used. Chris@0: $form['label']['#default_value'] = ''; Chris@0: $form['label']['#access'] = FALSE; Chris@0: Chris@0: // Unset the machine_name provided by BlockForm. Chris@0: unset($form['id']['#machine_name']['source']); Chris@0: // Prevent users from changing the auto-generated block machine_name. Chris@0: $form['id']['#access'] = FALSE; Chris@0: $form['#pre_render'][] = '\Drupal\views\Plugin\views\PluginBase::preRenderAddFieldsetMarkup'; Chris@0: Chris@0: // Allow to override the label on the actual page. Chris@0: $form['views_label_checkbox'] = [ Chris@0: '#type' => 'checkbox', Chris@0: '#title' => $this->t('Override title'), Chris@0: '#default_value' => !empty($this->configuration['views_label']), Chris@0: ]; Chris@0: Chris@0: $form['views_label_fieldset'] = [ Chris@0: '#type' => 'fieldset', Chris@0: '#states' => [ Chris@0: 'visible' => [ Chris@0: [ Chris@0: ':input[name="settings[views_label_checkbox]"]' => ['checked' => TRUE], Chris@0: ], Chris@0: ], Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $form['views_label'] = [ Chris@0: '#title' => $this->t('Title'), Chris@0: '#type' => 'textfield', Chris@0: '#default_value' => $this->configuration['views_label'] ?: $this->view->getTitle(), Chris@0: '#states' => [ Chris@0: 'visible' => [ Chris@0: [ Chris@0: ':input[name="settings[views_label_checkbox]"]' => ['checked' => TRUE], Chris@0: ], Chris@0: ], Chris@0: ], Chris@0: '#fieldset' => 'views_label_fieldset', Chris@0: ]; Chris@0: Chris@0: if ($this->view->storage->access('edit') && \Drupal::moduleHandler()->moduleExists('views_ui')) { Chris@18: $form['views_label']['#description'] = $this->t('Changing the title here means it cannot be dynamically altered anymore. (Try changing it directly in @name.)', [':url' => Url::fromRoute('entity.view.edit_display_form', ['view' => $this->view->storage->id(), 'display_id' => $this->displayID])->toString(), '@name' => $this->view->storage->label()]); Chris@0: } Chris@0: else { Chris@0: $form['views_label']['#description'] = $this->t('Changing the title here means it cannot be dynamically altered anymore.'); Chris@0: } Chris@0: Chris@0: return $form; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function blockSubmit($form, FormStateInterface $form_state) { Chris@0: if (!$form_state->isValueEmpty('views_label_checkbox')) { Chris@0: $this->configuration['views_label'] = $form_state->getValue('views_label'); Chris@0: } Chris@0: else { Chris@0: $this->configuration['views_label'] = ''; Chris@0: } Chris@0: $form_state->unsetValue('views_label_checkbox'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Converts Views block content to a renderable array with contextual links. Chris@0: * Chris@0: * @param string|array $output Chris@0: * An string|array representing the block. This will be modified to be a Chris@0: * renderable array, containing the optional '#contextual_links' property (if Chris@0: * there are any contextual links associated with the block). Chris@0: * @param string $block_type Chris@0: * The type of the block. If it's 'block' it's a regular views display, Chris@0: * but 'exposed_filter' exist as well. Chris@0: */ Chris@0: protected function addContextualLinks(&$output, $block_type = 'block') { Chris@0: // Do not add contextual links to an empty block. Chris@0: if (!empty($output)) { Chris@0: // Contextual links only work on blocks whose content is a renderable Chris@0: // array, so if the block contains a string of already-rendered markup, Chris@0: // convert it to an array. Chris@0: if (is_string($output)) { Chris@0: $output = ['#markup' => $output]; Chris@0: } Chris@0: Chris@0: // views_add_contextual_links() needs the following information in Chris@0: // order to be attached to the view. Chris@0: $output['#view_id'] = $this->view->storage->id(); Chris@0: $output['#view_display_show_admin_links'] = $this->view->getShowAdminLinks(); Chris@0: $output['#view_display_plugin_id'] = $this->view->display_handler->getPluginId(); Chris@0: views_add_contextual_links($output, $block_type, $this->displayID); Chris@0: } Chris@0: } Chris@0: Chris@18: /** Chris@18: * Gets the view executable. Chris@18: * Chris@18: * @return \Drupal\views\ViewExecutable Chris@18: * The view executable. Chris@18: * Chris@18: * @todo revisit after https://www.drupal.org/node/3027653. This method was Chris@18: * added in https://www.drupal.org/node/3002608, but should not be Chris@18: * necessary once block plugins can determine if they are being previewed. Chris@18: */ Chris@18: public function getViewExecutable() { Chris@18: return $this->view; Chris@18: } Chris@18: Chris@0: }