Skip to content

Instantly share code, notes, and snippets.

@heddn
Last active December 1, 2016 18:57
Show Gist options
  • Save heddn/8a477c4643678d7d7369ffbc15f3b170 to your computer and use it in GitHub Desktop.
Save heddn/8a477c4643678d7d7369ffbc15f3b170 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\opp_link\Access;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\Routing\Route;
/**
* Determines access to for link retrieve pages.
*/
class NodeLinkRetrieveAccessCheck implements AccessInterface {
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Constructs a EntityCreateAccessCheck object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
*/
public function __construct(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
/**
* Checks access to the entity operation on the given route.
*
* @param \Symfony\Component\Routing\Route $route
* The route to check against.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* The parametrized route
* @param \Drupal\Core\Session\AccountInterface $account
* The currently logged in account.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function access(Route $route, RouteMatchInterface $routeMatch, AccountInterface $account) {
$requirement = $route->getRequirement('_entity_access');
list($entity_type) = explode('.', $requirement);
$entity = $routeMatch->getParameter($entity_type);
$entity = $entity instanceof EntityInterface ? $entity : $this->entityManager->getStorage($entity_type)->load($entity);
return AccessResult::allowedIf($entity && $this->checkAccess($routeMatch, $entity))->cachePerPermissions()->addCacheableDependency($entity);
}
/**
* Checks if node is a link bundle.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* The parametrized route
* @param \Drupal\Core\Entity\EntityInterface $entity
* The node to check.
*
* @return bool
* TRUE if the operation may be performed, FALSE otherwise.
*/
public function checkAccess(RouteMatchInterface $routeMatch, EntityInterface $entity) {
return $routeMatch->getRouteName() == 'opp_link.entity.node.retrieve' && $entity->bundle() == 'link';
}
}
<?php
namespace Drupal\opp_link\Form;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\opp_link\Service\PrepopulateEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class UrlReRetrieve.
*
* @package Drupal\opp_link\Form
*/
class UrlReRetrieve extends ConfirmFormBase {
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/** @var \Drupal\opp_link\Service\PrepopulateEntityInterface */
protected $prepopulateEntity;
/** @var \Drupal\Core\Entity\EntityInterface */
protected $entity;
/** @var string Entity type */
protected $entityType = 'node';
/** @var string Bundle type */
protected $bundle = 'link';
/**
* Constructs a UrlRetrieve object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\opp_link\Service\PrepopulateEntityInterface $prepopulateEntity
* The entity prepopulate service..
*/
public function __construct(EntityManagerInterface $entityManager, PrepopulateEntityInterface $prepopulateEntity) {
$this->entityManager = $entityManager;
$this->prepopulateEntity = $prepopulateEntity;
}
/**
* @inheritDoc
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager'),
$container->get('opp_link.prepopulate_entity')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'opp_link_url_reretrieve';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$entity_id = $this->getRouteMatch()->getParameter($this->entityType);
$this->entity = $this->entityManager->getStorage($this->entityType)->load($entity_id);
/** @var \Drupal\link\Plugin\Field\FieldType\LinkItem $url */
$url = $this->entity->field_link->get(0);
$form['url'] = [
'#type' => 'url',
'#title' => $this->t('Url'),
'#default_value' => !empty($url) ? $url->getUrl()->toString() : '',
'#description' => $this->t('Please provide a url to re-seed the content.'),
'#required' => TRUE,
];
return parent::buildForm($form, $form_state);
}
/**
* @inheritDoc
*/
public function validateForm(array &$form, FormStateInterface $formState) {
parent::validateForm($form, $formState);
$url = $formState->getValues()['url'];
$body = $this->prepopulateEntity->retrieve($url);
if (!$body) {
$formState->setErrorByName('url', $this->t('The url provided cannot be processed.'));
}
$formState->setValue('retrieved_body', $body);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $formState) {
$url = $formState->getValues()['url'];
$body = $formState->getValues()['retrieved_body'];
$values = $this->prepopulateEntity->parse($body);
$values['id'] = $this->entity->id();
$values['entity_type'] = $this->entityType;
$values['type'] = $this->bundle;
$values['field_link'] = $url;
$this->prepopulateEntity->updateEntity($values);
$formState->setRedirectUrl($this->getRedirectUrl());
}
/**
* @inheritDoc
*/
public function getQuestion() {
$this->t('Do you want to re-retrieve the content for this page?');
}
/**
* @inheritDoc
*/
public function getCancelUrl() {
return $this->getRedirectUrl();
}
/**
* Returns the URL where the user should be redirected after submitting.
*
* @return \Drupal\Core\Url
* The redirect URL.
*/
protected function getRedirectUrl() {
// If available, return the edit-form URL.
if ($this->entity->hasLinkTemplate('edit-form')) {
return $this->entity->toUrl('edit-form');
}
else {
// Otherwise fall back to the front page.
return Url::fromRoute('<front>');
}
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('This action cannot be undone.');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Confirm');
}
/**
* {@inheritdoc}
*/
public function getCancelText() {
return $this->t('Cancel');
}
/**
* {@inheritdoc}
*/
public function getFormName() {
return 'confirm';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment