Loading src/Controller/WeatherUserConfiguredDisplayController.php 0 → 100644 +142 −0 Original line number Diff line number Diff line <?php namespace Drupal\weather\Controller; use Drupal\Core\Access\AccessResult; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Link; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Drupal\user\UserInterface; use Drupal\weather\Entity\WeatherDisplayInterface; use Drupal\weather\Service\HelperService; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Returns responses for Weather routes. */ class WeatherUserConfiguredDisplayController extends ControllerBase { /** * The weather.helper service. * * @var \Drupal\weather\Service\HelperService */ protected $weatherHelper; /** * The entity.type_manager service. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * The controller constructor. * * @param \Drupal\weather\Service\HelperService $weather_helper * The weather.helper service. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity.type_manager service. */ public function __construct(HelperService $weather_helper, EntityTypeManagerInterface $entity_type_manager) { $this->weatherHelper = $weather_helper; $this->entityTypeManager = $entity_type_manager; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('weather.helper'), $container->get('entity_type.manager') ); } /** * Builds the response. */ public function content(UserInterface $user) { $weatherDisplayPlaceStorage = $this->entityTypeManager->getStorage('weather_display_place'); $header = [ $this->t('Displayed name'), $this->t('Weight'), ]; $rows = []; $result = $weatherDisplayPlaceStorage->getQuery() ->condition('display_type', WeatherDisplayInterface::USER_TYPE) ->condition('display_number', $user->id()) ->sort('weight', 'ASC') ->sort('displayed_name', 'ASC') ->execute(); if ($result) { foreach ($weatherDisplayPlaceStorage->loadMultiple($result) as $location) { $rows[] = [ Link::createFromRoute( $location->displayed_name->value, 'weather.user.weather_display_place.edit_form', [ 'display_number' => $user->id(), 'weather_display_place' => $location->id(), ]), $location->weight->value, ]; } } // Insert link for adding locations into the table as last row. $rows[] = [ [ 'data' => Link::createFromRoute($this->t('Add location to this display'), 'weather.user.weather_display_place.add_form', ['display_number' => $user->id()]), 'colspan' => 2, ], ]; $output['table'] = [ '#theme' => 'table', '#header' => $header, '#rows' => $rows, ]; // Generate link to Add or Edit user's weather display. $url = Url::fromRoute('weather.user.weather_display.add_form', ['user' => $user->id()]); $weatherDisplayStorage = $this->entityTypeManager->getStorage('weather_display'); $userWeatherDisplay = $weatherDisplayStorage->loadByProperties([ 'type' => WeatherDisplayInterface::USER_TYPE, 'number' => $user->id(), ]); $userWeatherDisplay = reset($userWeatherDisplay); if ($userWeatherDisplay instanceof WeatherDisplayInterface) { $url = Url::fromRoute('weather.user.weather_display.edit_form', ['display_number' => $user->id(), 'weather_display' => $userWeatherDisplay->id()]); } $output['edit_display'] = [ '#type' => 'link', '#title' => $this->t('Edit configuration of display'), '#url' => $url, ]; return $output; } /** * Checks access for a specific request. * * @param \Drupal\Core\Session\AccountInterface $account * Run access checks for this account. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */ public function access(AccountInterface $account) { return AccessResult::allowedIf( $this->currentUser()->id() == $account->id() && $account->hasPermission('administer custom weather block') ); } } src/Entity/WeatherDisplay.php +3 −0 Original line number Diff line number Diff line Loading @@ -34,6 +34,9 @@ use Drupal\Core\Field\BaseFieldDefinition; * entity_keys = { * "id" = "id", * }, * constraints = { * "WeatherDisplay" = {}, * } * ) */ class WeatherDisplay extends ContentEntityBase implements WeatherDisplayInterface { Loading src/Entity/WeatherDisplayPlace.php +24 −0 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ namespace Drupal\weather\Entity; use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; Loading Loading @@ -79,4 +80,27 @@ class WeatherDisplayPlace extends ContentEntityBase implements WeatherDisplayPla return $fields; } /** * {@inheritdoc} */ public function preSave(EntityStorageInterface $storage) { // Make sure display for this place exists. $displayStorage = $this->entityTypeManager()->getStorage('weather_display'); $display = $displayStorage->loadByProperties([ 'type' => WeatherDisplayInterface::USER_TYPE, 'number' => $this->display_number->value, ]); if (empty($display)) { $values = [ 'config' => \Drupal::service('weather.helper')->getDefaultConfig(), 'type' => WeatherDisplayInterface::USER_TYPE, 'number' => $this->display_number->value, ]; $displayStorage->create($values)->save(); } parent::preSave($storage); } } src/Entity/WeatherDisplayPlaceInterface.php +1 −3 Original line number Diff line number Diff line Loading @@ -9,6 +9,4 @@ use Drupal\Core\Entity\ContentEntityInterface; * * @ingroup weather */ interface WeatherDisplayPlaceInterface extends ContentEntityInterface { } interface WeatherDisplayPlaceInterface extends ContentEntityInterface {} src/Form/WeatherDisplayDeleteForm.php +14 −1 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\weather\Form; use Drupal\Core\Entity\ContentEntityConfirmFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\weather\Entity\WeatherDisplayInterface; /** * Provides a form for deleting a weather_display entity. Loading Loading @@ -38,9 +39,21 @@ class WeatherDisplayDeleteForm extends ContentEntityConfirmFormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $display_type = $this->entity->type->value; $display_number = $this->entity->number->value; $this->entity->delete(); $this->messenger()->addStatus($this->t('Weather display was removed.')); $form_state->setRedirect('weather.settings'); switch ($display_type) { case WeatherDisplayInterface::USER_TYPE: $form_state->setRedirectUrl(Url::fromRoute('weather.user.settings', ['user' => $display_number])); break; default: $form_state->setRedirectUrl(Url::fromRoute('weather.settings')); break; } parent::submitForm($form, $form_state); } Loading Loading
src/Controller/WeatherUserConfiguredDisplayController.php 0 → 100644 +142 −0 Original line number Diff line number Diff line <?php namespace Drupal\weather\Controller; use Drupal\Core\Access\AccessResult; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Link; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Drupal\user\UserInterface; use Drupal\weather\Entity\WeatherDisplayInterface; use Drupal\weather\Service\HelperService; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Returns responses for Weather routes. */ class WeatherUserConfiguredDisplayController extends ControllerBase { /** * The weather.helper service. * * @var \Drupal\weather\Service\HelperService */ protected $weatherHelper; /** * The entity.type_manager service. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * The controller constructor. * * @param \Drupal\weather\Service\HelperService $weather_helper * The weather.helper service. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity.type_manager service. */ public function __construct(HelperService $weather_helper, EntityTypeManagerInterface $entity_type_manager) { $this->weatherHelper = $weather_helper; $this->entityTypeManager = $entity_type_manager; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('weather.helper'), $container->get('entity_type.manager') ); } /** * Builds the response. */ public function content(UserInterface $user) { $weatherDisplayPlaceStorage = $this->entityTypeManager->getStorage('weather_display_place'); $header = [ $this->t('Displayed name'), $this->t('Weight'), ]; $rows = []; $result = $weatherDisplayPlaceStorage->getQuery() ->condition('display_type', WeatherDisplayInterface::USER_TYPE) ->condition('display_number', $user->id()) ->sort('weight', 'ASC') ->sort('displayed_name', 'ASC') ->execute(); if ($result) { foreach ($weatherDisplayPlaceStorage->loadMultiple($result) as $location) { $rows[] = [ Link::createFromRoute( $location->displayed_name->value, 'weather.user.weather_display_place.edit_form', [ 'display_number' => $user->id(), 'weather_display_place' => $location->id(), ]), $location->weight->value, ]; } } // Insert link for adding locations into the table as last row. $rows[] = [ [ 'data' => Link::createFromRoute($this->t('Add location to this display'), 'weather.user.weather_display_place.add_form', ['display_number' => $user->id()]), 'colspan' => 2, ], ]; $output['table'] = [ '#theme' => 'table', '#header' => $header, '#rows' => $rows, ]; // Generate link to Add or Edit user's weather display. $url = Url::fromRoute('weather.user.weather_display.add_form', ['user' => $user->id()]); $weatherDisplayStorage = $this->entityTypeManager->getStorage('weather_display'); $userWeatherDisplay = $weatherDisplayStorage->loadByProperties([ 'type' => WeatherDisplayInterface::USER_TYPE, 'number' => $user->id(), ]); $userWeatherDisplay = reset($userWeatherDisplay); if ($userWeatherDisplay instanceof WeatherDisplayInterface) { $url = Url::fromRoute('weather.user.weather_display.edit_form', ['display_number' => $user->id(), 'weather_display' => $userWeatherDisplay->id()]); } $output['edit_display'] = [ '#type' => 'link', '#title' => $this->t('Edit configuration of display'), '#url' => $url, ]; return $output; } /** * Checks access for a specific request. * * @param \Drupal\Core\Session\AccountInterface $account * Run access checks for this account. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */ public function access(AccountInterface $account) { return AccessResult::allowedIf( $this->currentUser()->id() == $account->id() && $account->hasPermission('administer custom weather block') ); } }
src/Entity/WeatherDisplay.php +3 −0 Original line number Diff line number Diff line Loading @@ -34,6 +34,9 @@ use Drupal\Core\Field\BaseFieldDefinition; * entity_keys = { * "id" = "id", * }, * constraints = { * "WeatherDisplay" = {}, * } * ) */ class WeatherDisplay extends ContentEntityBase implements WeatherDisplayInterface { Loading
src/Entity/WeatherDisplayPlace.php +24 −0 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ namespace Drupal\weather\Entity; use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; Loading Loading @@ -79,4 +80,27 @@ class WeatherDisplayPlace extends ContentEntityBase implements WeatherDisplayPla return $fields; } /** * {@inheritdoc} */ public function preSave(EntityStorageInterface $storage) { // Make sure display for this place exists. $displayStorage = $this->entityTypeManager()->getStorage('weather_display'); $display = $displayStorage->loadByProperties([ 'type' => WeatherDisplayInterface::USER_TYPE, 'number' => $this->display_number->value, ]); if (empty($display)) { $values = [ 'config' => \Drupal::service('weather.helper')->getDefaultConfig(), 'type' => WeatherDisplayInterface::USER_TYPE, 'number' => $this->display_number->value, ]; $displayStorage->create($values)->save(); } parent::preSave($storage); } }
src/Entity/WeatherDisplayPlaceInterface.php +1 −3 Original line number Diff line number Diff line Loading @@ -9,6 +9,4 @@ use Drupal\Core\Entity\ContentEntityInterface; * * @ingroup weather */ interface WeatherDisplayPlaceInterface extends ContentEntityInterface { } interface WeatherDisplayPlaceInterface extends ContentEntityInterface {}
src/Form/WeatherDisplayDeleteForm.php +14 −1 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\weather\Form; use Drupal\Core\Entity\ContentEntityConfirmFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\weather\Entity\WeatherDisplayInterface; /** * Provides a form for deleting a weather_display entity. Loading Loading @@ -38,9 +39,21 @@ class WeatherDisplayDeleteForm extends ContentEntityConfirmFormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $display_type = $this->entity->type->value; $display_number = $this->entity->number->value; $this->entity->delete(); $this->messenger()->addStatus($this->t('Weather display was removed.')); $form_state->setRedirect('weather.settings'); switch ($display_type) { case WeatherDisplayInterface::USER_TYPE: $form_state->setRedirectUrl(Url::fromRoute('weather.user.settings', ['user' => $display_number])); break; default: $form_state->setRedirectUrl(Url::fromRoute('weather.settings')); break; } parent::submitForm($form, $form_state); } Loading