messenger example


  public function commentApprove(CommentInterface $comment) {
    $comment->setPublished();
    $comment->save();

    $this->messenger()->addStatus($this->t('Comment approved.'));
    $permalink_uri = $comment->permalink();
    $permalink_uri->setAbsolute();
    return new RedirectResponse($permalink_uri->toString());
  }

  /** * Redirects comment links to the correct page depending on comment settings. * * Since comments are paged there is no way to guarantee which page a comment * appears on. Comment paging and threading settings may be changed at any * time. With threaded comments, an individual comment may move between pages * as comments can be added either before or after it in the overall * discussion. Therefore we use a central routing function for comment links, * which calculates the page number based on current comment settings and * returns the full comment view with the pager set dynamically. * * @param \Symfony\Component\HttpFoundation\Request $request * The request of the page. * @param \Drupal\comment\CommentInterface $comment * A comment entity. * * @return \Symfony\Component\HttpFoundation\Response * The comment listing set to the page on which the comment appears. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException */
$default_value = $items->defaultValuesFormSubmit($form['default_value']$form$form_state);
    }
    $this->entity->setDefaultValue($default_value);
  }

  /** * {@inheritdoc} */
  public function save(array $form, FormStateInterface $form_state) {
    $this->entity->save();

    $this->messenger()->addStatus($this->t('Saved %label configuration.', ['%label' => $this->entity->getLabel()]));

    $request = $this->getRequest();
    if (($destinations = $request->query->all('destinations')) && $next_destination = FieldUI::getNextDestination($destinations)) {
      $request->query->remove('destinations');
      $form_state->setRedirectUrl($next_destination);
    }
    else {
      $form_state->setRedirectUrl(FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId()$this->entity->getTargetBundle()));
    }
  }

  

  public function getCancelUrl() {
    return new Url('<current>');
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->workspaceManager->switchToLive();
    $this->messenger()->addMessage($this->t('You are now viewing the live version of the site.'));
  }

}
// @todo Consider using 'fetch_failures' from the 'update' collection       // in the key_value_expire service for this?       $fetch_failed = FALSE;
      foreach ($build['#data'] as $project) {
        if ($project['status'] === UpdateFetcherInterface::NOT_FETCHED) {
          $fetch_failed = TRUE;
          break;
        }
      }
      if ($fetch_failed) {
        $message = ['#theme' => 'update_fetch_error_message'];
        $this->messenger()->addError($this->renderer->renderPlain($message));
      }
    }
    return $build;
  }

  /** * Manually checks the update status without the use of cron. */
  public function updateStatusManually() {
    $this->updateManager->refreshUpdateData();
    $batch_builder = (new BatchBuilder())
      
    if (!$entity->isDefaultTranslation()) {
      $untranslated_entity = $entity->getUntranslated();
      $untranslated_entity->removeTranslation($entity->language()->getId());
      $untranslated_entity->save();
      $form_state->setRedirectUrl($untranslated_entity->toUrl('canonical'));
    }
    else {
      $entity->delete();
      $form_state->setRedirectUrl($this->getRedirectUrl());
    }

    $this->messenger()->addStatus($message);
    $this->logDeletionMessage();
  }

  /** * {@inheritdoc} */
  public function getCancelUrl() {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->getEntity();
    return $entity->isDefaultTranslation() ? $this->traitGetCancelUrl() : $entity->toUrl('canonical');
  }

  
    return new Response('', 204);
  }

  /** * Run cron manually. * * @return \Symfony\Component\HttpFoundation\RedirectResponse * A Symfony direct response object. */
  public function runManually() {
    if ($this->cron->run()) {
      $this->messenger()->addStatus($this->t('Cron ran successfully.'));
    }
    else {
      $this->messenger()->addError($this->t('Cron run failed.'));
    }

    return $this->redirect('system.status');
  }

}

  public function save(array $form, FormStateInterface $form_state) {
    $term = $this->entity;

    $result = $term->save();

    $edit_link = $term->toLink($this->t('Edit'), 'edit-form')->toString();
    $view_link = $term->toLink()->toString();
    switch ($result) {
      case SAVED_NEW:
        $this->messenger()->addStatus($this->t('Created new term %term.', ['%term' => $view_link]));
        $this->logger('taxonomy')->notice('Created new term %term.', ['%term' => $term->getName(), 'link' => $edit_link]);
        break;

      case SAVED_UPDATED:
        $this->messenger()->addStatus($this->t('Updated term %term.', ['%term' => $view_link]));
        $this->logger('taxonomy')->notice('Updated term %term.', ['%term' => $term->getName(), 'link' => $edit_link]);
        $form_state->setRedirect('entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()]);
        break;
    }

    $current_parent_count = count($form_state->getValue('parent'));
    
// Log the search.         if ($this->config('search.settings')->get('logging')) {
          $this->logger->notice('Searched %type for %keys.', ['%keys' => $keys, '%type' => $entity->label()]);
        }

        // Collect the search results.         $results = $plugin->buildResults();
      }
      else {
        // The search not being executable means that no keywords or other         // conditions were entered.         $this->messenger()->addError($this->t('Please enter some keywords.'));
      }
    }

    if (count($results)) {
      $build['search_results_title'] = [
        '#markup' => '<h2>' . $this->t('Search results') . '</h2>',
      ];
    }

    $build['search_results'] = [
      '#theme' => ['item_list__search_results__' . $plugin->getPluginId(), 'item_list__search_results'],
      
if (parse_url($link, PHP_URL_SCHEME) === NULL && $this->pathValidator->isValid($link)) {
      $shortcut = $this->entityTypeManager()->getStorage('shortcut')->create([
        'title' => $name,
        'shortcut_set' => $shortcut_set->id(),
        'link' => [
          'uri' => 'internal:/' . $link,
        ],
      ]);

      try {
        $shortcut->save();
        $this->messenger()->addStatus($this->t('Added a shortcut for %title.', ['%title' => $shortcut->label()]));
      }
      catch (\Exception $e) {
        $this->messenger()->addError($this->t('Unable to add a shortcut for %title.', ['%title' => $shortcut->label()]));
      }

      return $this->redirect('<front>');
    }

    throw new AccessDeniedHttpException();
  }

}

  protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $limit = 0, $ids = NULL) {
    $view_name = $this->getConfiguration()['view']['view_name'];
    $display_name = $this->getConfiguration()['view']['display_name'];

    // Check that the view is valid and the display still exists.     $this->view = Views::getView($view_name);
    if (!$this->view || !$this->view->access($display_name)) {
      \Drupal::messenger()->addWarning($this->t('The reference view %view_name cannot be found.', ['%view_name' => $view_name]));
      return FALSE;
    }
    $this->view->setDisplay($display_name);

    // Pass options to the display handler to make them available later.     $entity_reference_options = [
      'match' => $match,
      'match_operator' => $match_operator,
      'limit' => $limit,
      'ids' => $ids,
    ];
    
$this->imageStyle = $image_style;
    $this->imageEffect = $this->imageStyle->getEffect($image_effect);

    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->imageStyle->deleteImageEffect($this->imageEffect);
    $this->messenger()->addStatus($this->t('The image effect %name has been deleted.', ['%name' => $this->imageEffect->label()]));
    $form_state->setRedirectUrl($this->imageStyle->toUrl('edit-form'));
  }

}

    // If there's no form, immediately add the image effect.     else {
      $effect = [
        'id' => $effect['id'],
        'data' => [],
        'weight' => $form_state->getValue('weight'),
      ];
      $effect_id = $this->entity->addImageEffect($effect);
      $this->entity->save();
      if (!empty($effect_id)) {
        $this->messenger()->addStatus($this->t('The image effect was successfully applied.'));
      }
    }
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Update image effect weights.     if (!$form_state->isValueEmpty('effects')) {
      
/** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state) {
    return $this->tableselectFormBuilder($form$form_state['#multiple' => FALSE]);
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->messenger()->addStatus($this->t('Submitted: @value', ['@value' => $form_state->getValue('tableselect')]));
  }

}
// Set configuration values based on form submission and source values.       $base_config = $this->configFactory()->getEditable($name);
      $config_translation = $this->languageManager->getLanguageConfigOverride($this->language->getId()$name);

      $element = $this->createFormElement($schema);
      $element->setConfig($base_config$config_translation$value);

      // If no overrides, delete language specific configuration file.       $saved_config = $config_translation->get();
      if (empty($saved_config)) {
        $config_translation->delete();
        $this->messenger()->addStatus($this->t('@language translation was not added. To add a translation, you must modify the configuration.', ['@language' => $this->language->getName()]));
      }
      else {
        $config_translation->save();
        $this->messenger()->addStatus($this->t('Successfully saved @language translation.', ['@language' => $this->language->getName()]));
      }
    }

    $form_state->setRedirect(
      $this->mapper->getOverviewRouteName(),
      $this->mapper->getOverviewRouteParameters()
    );
  }
$text = $this->t("This update will be skipped due to an error in the module's code.");
          }
          $build['start'][$module_update_key]['#items'][$data['number']] .= '<div class="warning">' . $text . '</div>';
        }
        // Move the module containing this update to the top of the list.         $build['start'] = [$module_update_key => $build['start'][$module_update_key]] + $build['start'];
      }
    }

    // Warn the user if any updates were incompatible.     if ($incompatible_updates_exist) {
      $this->messenger()->addWarning($this->t('Some of the pending updates cannot be applied because their dependencies were not met.'));
    }

    if (empty($count)) {
      $this->messenger()->addStatus($this->t('No pending updates.'));
      unset($build);
      $build['links'] = [
        '#theme' => 'links',
        '#links' => $this->helpfulLinks($request),
      ];

      // No updates to run, so caches won't get flushed later. Clear them now.
Home | Imprint | This part of the site doesn't use cookies.