Browse Source

Sprint August - Sept 2016 (#4)

* Sprint Sept 2016

Huge Pull Request,
Highlights:
 * removed rdf_type from fedora_source_type entity, better to handle
this via rdfmapping based on bundle rdf assignment
 * Added inline form support
 * Added default rdf namespace register
 * Added test rdf.map config entity (have to check if it’s working,
probably some typo?
 * Added experimental UUID Entity Solver(service, route and @annotation
link
 * Made jsonld serialiser (github.com/diegopino/claw-jsonld) a
requirement
 * Some route cleanups

* Updated to correct Branch

* Bad cherry picked

Double method declaration after cherry picking, fixed.
pull/756/head
Diego Pino Navarro 8 years ago committed by dannylamb
parent
commit
3158fc97aa
  1. 26
      README.md
  2. 1
      config/install/islandora.fedora_resource_type.non_rdf_source.yml
  3. 1
      config/install/islandora.fedora_resource_type.rdf_source.yml
  4. 36
      config/install/rdf.mapping.fedora_resource_type.rdf_source.yml
  5. 3
      config/schema/fedora_resource_type.schema.yml
  6. 13
      fedora_resource.page.inc
  7. 10
      islandora.info.yml
  8. 1
      islandora.links.action.yml
  9. 1
      islandora.links.task.yml
  10. 28
      islandora.module
  11. 25
      islandora.routing.yml
  12. 8
      islandora.services.yml
  13. 54
      src/Entity/FedoraResource.php
  14. 15
      src/Entity/FedoraResourceType.php
  15. 1
      src/FedoraResourceAccessControlHandler.php
  16. 37
      src/FedoraResourceHtmlRouteProvider.php
  17. 1
      src/FedoraResourceTypeHtmlRouteProvider.php
  18. 5
      src/FedoraResourceTypeInterface.php
  19. 2
      src/FedoraResourceTypeListBuilder.php
  20. 26
      src/FedoraResourceViewBuilder.php
  21. 279
      src/Form/FedoraResourceEntityInlineForm.php
  22. 9
      src/Form/FedoraResourceTypeForm.php
  23. 69
      src/ParamConverter/UuidEntityConverter.php

26
README.md

@ -1 +1,25 @@
# claw-drupal8 # Islandora
[![Build Status](https://travis-ci.org/Islandora-CLAW/islandora.png?branch=8.x-1.x)](https://travis-ci.org/Islandora-CLAW/islandora)
[![Contribution Guidelines](http://img.shields.io/badge/CONTRIBUTING-Guidelines-blue.svg)](./CONTRIBUTING.md)
[![LICENSE](https://img.shields.io/badge/license-GPLv2-blue.svg?style=flat-square)](./LICENSE)
## Introduction
Islandora Fedora 4 Repository Module for Drupal 8.1.x
## Maintainers
Current maintainers:
* [Diego Pino](https://github.com/diegopino)
* [Jared Whiklo](https://github.com/whikloj)
## Development
If you would like to contribute, please get involved by attending our weekly [Tech Call](https://github.com/Islandora-CLAW/CLAW/wiki). We love to hear from you!
If you would like to contribute code to the project, you need to be covered by an Islandora Foundation [Contributor License Agreement](http://islandora.ca/sites/default/files/islandora_cla.pdf) or [Corporate Contributor Licencse Agreement](http://islandora.ca/sites/default/files/islandora_ccla.pdf). Please see the [Contributors](http://islandora.ca/resources/contributors) pages on Islandora.ca for more information.
## License
[GPLv2](http://www.gnu.org/licenses/gpl-2.0.txt)

1
config/install/islandora.fedora_resource_type.non_rdf_source.yml

@ -1,4 +1,3 @@
langcode: en langcode: en
id: non_rdf_source id: non_rdf_source
label: 'Non RDF Source' label: 'Non RDF Source'
rdf_type: fedora:Binary

1
config/install/islandora.fedora_resource_type.rdf_source.yml

@ -1,4 +1,3 @@
langcode: en langcode: en
id: rdf_source id: rdf_source
label: 'RDF Source' label: 'RDF Source'
rdf_type: fedora:Resource

36
config/install/rdf.mapping.fedora_resource_type.rdf_source.yml

@ -0,0 +1,36 @@
langcode: en
status: true
dependencies:
config:
- islandora.fedora_resource_type.rdf_source
module:
- islandora
id: fedora_resource_type.rdf_source
targetEntityType: fedora_resource_type
bundle: rdf_source
types:
- 'schema:Thing'
- 'ldp:RDFSource'
- 'ldp:Container'
fieldMappings:
title:
properties:
- 'dc11:title'
- 'rdf:label'
created:
properties:
- 'schema:dateCreated'
datatype_callback:
callable: 'Drupal\rdf\CommonDataConverter::dateIso8601Value'
changed:
properties:
- 'schema:dateModified'
datatype_callback:
callable: 'Drupal\rdf\CommonDataConverter::dateIso8601Value'
rdf_type:
properties:
- 'rdf:type'
uid:
properties:
- 'schema:author'
mapping_type: rel

3
config/schema/fedora_resource_type.schema.yml

@ -10,9 +10,6 @@ islandora.fedora_resource_type.*:
label: 'Label' label: 'Label'
uuid: uuid:
type: string type: string
rdf_type:
type: label
label: 'RDF type'
# Plugin \Drupal\islandora\Plugin\Search\FedoraResourceSearch # Plugin \Drupal\islandora\Plugin\Search\FedoraResourceSearch
search.plugin.node_search: search.plugin.node_search:

13
fedora_resource.page.inc

@ -5,8 +5,17 @@
* Contains fedora_resource.page.inc. * Contains fedora_resource.page.inc.
* *
* Page callback for Fedora resource entities. * Page callback for Fedora resource entities.
*
* This file is part of the Islandora Project.
*
* (c) Islandora Foundation
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Diego Pino Navarro <dpino@metro.org> https://github.com/diegopino
*
*/ */
use Drupal\Core\Render\Element; use Drupal\Core\Render\Element;
use Drupal\Core\Link; use Drupal\Core\Link;
use Drupal\Core\Url; use Drupal\Core\Url;
@ -48,7 +57,7 @@ function template_preprocess_fedora_resource_content_add_list(&$variables) {
foreach ($variables['content'] as $type) { foreach ($variables['content'] as $type) {
$variables['types'][$type->id()] = array( $variables['types'][$type->id()] = array(
'link' => Link::fromTextAndUrl($type->label(), new Url('entity.fedora_resource.add_form', array( 'link' => Link::fromTextAndUrl($type->label(), new Url('entity.fedora_resource.add_form', array(
'fedora_resource_type' => $type->id() 'fedora_resource_type' => $type->id(),
), array('query' => $query))), ), array('query' => $query))),
'description' => array( 'description' => array(
'#markup' => $type->label(), '#markup' => $type->label(),

10
islandora.info.yml

@ -1,9 +1,9 @@
# This .info.yml files provides the basic information about our module to Drupal # This .info.yml files provides the basic information about our module to Drupal
# More: https://www.drupal.org/node/2000204 # More: https://www.drupal.org/node/2000204
name: Islandora CLAW name: 'islandora'
description: 'Learning Drupal 8 with Islandora Claw' description: "Drupal 8 islandora claw playground"
type: module type: module
package: Islandora package: islandora
core: 8.x core: 8.x
dependencies: dependencies:
- block - block
@ -11,4 +11,6 @@ dependencies:
- path - path
- text - text
- options - options
version: '8.x-2.x-dev' - inline_entity_form
- jsonld
version: '8.x-1.x-dev'

1
islandora.links.action.yml

@ -3,7 +3,6 @@ entity.fedora_resource.add_form:
title: 'Add Fedora resource' title: 'Add Fedora resource'
appears_on: appears_on:
- entity.fedora_resource.collection - entity.fedora_resource.collection
entity.fedora_resource_type.add_form: entity.fedora_resource_type.add_form:
route_name: 'entity.fedora_resource_type.add_form' route_name: 'entity.fedora_resource_type.add_form'
title: 'Add Fedora resource type' title: 'Add Fedora resource type'

1
islandora.links.task.yml

@ -14,4 +14,3 @@ entity.fedora_resource.delete_form:
base_route: entity.fedora_resource.canonical base_route: entity.fedora_resource.canonical
title: Delete title: Delete
weight: 10 weight: 10

28
islandora.module

@ -2,7 +2,16 @@
/** /**
* @file * @file
* Contains islandora.module.. * Contains islandora.module.
*
* This file is part of the Islandora Project.
*
* (c) Islandora Foundation
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Diego Pino Navarro <dpino@metro.org> https://github.com/diegopino
*/ */
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
@ -56,3 +65,20 @@ function islandora_theme_suggestions_fedora_resource(array $variables) {
$suggestions[] = 'fedora_resource__' . $entity->id() . '__' . $sanitized_view_mode; $suggestions[] = 'fedora_resource__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions; return $suggestions;
} }
/**
* Implements hook_rdf_namespaces().
*/
function islandora_rdf_namespaces() {
// Yes, it's amazing, rdf is not registered by default!
return array(
'ldp' => 'http://www.w3.org/ns/ldp#',
'dc11' => 'http://purl.org/dc/elements/1.1/',
'nfo' => 'http://www.semanticdesktop.org/ontologies/2007/03/22/nfo/v1.1/',
'ebucore' => 'http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#',
'fedora' => 'http://fedora.info/definitions/v4/repository#',
'owl' => 'http://www.w3.org/2002/07/owl#',
'ore' => 'http://www.openarchives.org/ore/terms/',
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
);
}

25
islandora.routing.yml

@ -1,19 +1,12 @@
entity.fedora_resource.collection: # Islandora Routing definition
path: '/admin/structure/fedora_resource' entity.fedora_resource_type.rdftest:
path: '/fedora_resource/{fedora_resource}/rdf'
defaults: defaults:
_entity_list: 'fedora_resource' _controller: '\Drupal\node\Controller\NodePreviewController::view'
_title: 'Fedora Resource' _title_callback: '\Drupal\node\Controller\NodePreviewController::title'
requirements: requirements:
_permission: 'administer fedora resource entities' _node_preview_access: '{node_preview}'
options: options:
_admin_route: true parameters:
node_preview:
entity.fedora_resource_type.collection: type: 'node_preview'
path: '/admin/structure/fedora_resource_type'
defaults:
_entity_list: 'fedora_resource_type'
_title: 'Fedora Resource Type'
requirements:
_permission: 'administer fedora resource type entities'
options:
_admin_route: true

8
islandora.services.yml

@ -0,0 +1,8 @@
# Islandora Services
# islandora.services.yml:
services:
islandora.paramconverter.entity:
class: Drupal\islandora\ParamConverter\UuidEntityConverter
tags:
- { name: paramconverter }
arguments: ['@entity.manager']

54
src/Entity/FedoraResource.php

@ -23,7 +23,7 @@ use Drupal\user\UserInterface;
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder", * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\islandora\FedoraResourceListBuilder", * "list_builder" = "Drupal\islandora\FedoraResourceListBuilder",
* "views_data" = "Drupal\islandora\Entity\FedoraResourceViewsData", * "views_data" = "Drupal\islandora\Entity\FedoraResourceViewsData",
* * "inline_form" = "Drupal\islandora\Form\FedoraResourceEntityInlineForm",
* "form" = { * "form" = {
* "default" = "Drupal\islandora\Form\FedoraResourceForm", * "default" = "Drupal\islandora\Form\FedoraResourceForm",
* "add" = "Drupal\islandora\Form\FedoraResourceForm", * "add" = "Drupal\islandora\Form\FedoraResourceForm",
@ -52,22 +52,53 @@ use Drupal\user\UserInterface;
* common_reference_target = TRUE, * common_reference_target = TRUE,
* permission_granularity = "bundle", * permission_granularity = "bundle",
* links = { * links = {
* "canonical" = "/admin/structure/fedora_resource/{fedora_resource}", * "canonical" = "/fedora_resource/{fedora_resource}",
* "add-form" = "/admin/structure/fedora_resource/add/{fedora_resource_type}", * "uuid" = "/islandora_uuid/{fedora_resource}",
* "edit-form" = "/admin/structure/fedora_resource/{fedora_resource}/edit", * "add-form" = "/fedora_resource/add/{fedora_resource_type}",
* "delete-form" = "/admin/structure/fedora_resource/{fedora_resource}/delete", * "edit-form" = "/fedora_resource/{fedora_resource}/edit",
* "delete-form" = "/fedora_resource/{fedora_resource}/delete",
* "collection" = "/admin/structure/fedora_resource", * "collection" = "/admin/structure/fedora_resource",
* "version-history" = "/admin/structure/{fedora_resource}/revisions",
* "revision" = "/admin/structure/{fedora_resource}/revisions/{node_revision}/view",
* }, * },
* bundle_entity_type = "fedora_resource_type", * bundle_entity_type = "fedora_resource_type",
* field_ui_base_route = "entity.fedora_resource_type.edit_form" * field_ui_base_route = "entity.fedora_resource_type.edit_form"
* ) * )
*/ */
class FedoraResource extends ContentEntityBase implements FedoraResourceInterface { class FedoraResource extends ContentEntityBase implements FedoraResourceInterface{
use EntityChangedTrait; use EntityChangedTrait;
/**
* @param string $rel
* @param array $options
* @return $this|\Drupal\Core\Url
*/
public function toUrl($rel = 'canonical', array $options = []) {
// TODO: I Will override this to have a rel ='uuid' for islandora entities
return parent::toUrl($rel, $options); // TODO: Change the autogenerated stub
}
/**
* Gets an array of placeholders for Fedora Resource Entity.
*
* We are overriding this method to add additional uuid
* placeholders.
*
* @param string $rel
* The link relationship type, for example: canonical or edit-form.
*
* @return array
* An array of URI placeholders.
*/
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel); // TODO: Change the autogenerated stub
if ($rel === 'uuid') {
$uri_route_parameters[$this->getEntityTypeId()] = $this->uuid();
}
return $uri_route_parameters;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -175,8 +206,6 @@ class FedoraResource extends ContentEntityBase implements FedoraResourceInterfac
$this->set('status', $published ? NODE_PUBLISHED : NODE_NOT_PUBLISHED); $this->set('status', $published ? NODE_PUBLISHED : NODE_NOT_PUBLISHED);
return $this; return $this;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -184,7 +213,6 @@ class FedoraResource extends ContentEntityBase implements FedoraResourceInterfac
{ {
return ($this->get('fedora_has_parent') !== null); return ($this->get('fedora_has_parent') !== null);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -192,7 +220,6 @@ class FedoraResource extends ContentEntityBase implements FedoraResourceInterfac
{ {
return $this->get('fedora_has_parent')->getEntity(); return $this->get('fedora_has_parent')->getEntity();
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -200,7 +227,6 @@ class FedoraResource extends ContentEntityBase implements FedoraResourceInterfac
{ {
return $this->get('fedora_has_parent')->getEntity()->id(); return $this->get('fedora_has_parent')->getEntity()->id();
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -219,9 +245,11 @@ class FedoraResource extends ContentEntityBase implements FedoraResourceInterfac
* An array of default values. * An array of default values.
*/ */
public static function getFedoraRoot() { public static function getFedoraRoot() {
// Just stub code, we need to figure out what "root is" in this context
return array('root'); return array('root');
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

15
src/Entity/FedoraResourceType.php

@ -31,7 +31,6 @@ use Drupal\islandora\FedoraResourceTypeInterface;
* "uuid" = "uuid" * "uuid" = "uuid"
* }, * },
* links = { * links = {
* "canonical" = "/admin/structure/fedora_resource_type/{fedora_resource_type}",
* "add-form" = "/admin/structure/fedora_resource_type/add", * "add-form" = "/admin/structure/fedora_resource_type/add",
* "edit-form" = "/admin/structure/fedora_resource_type/{fedora_resource_type}/edit", * "edit-form" = "/admin/structure/fedora_resource_type/{fedora_resource_type}/edit",
* "delete-form" = "/admin/structure/fedora_resource_type/{fedora_resource_type}/delete", * "delete-form" = "/admin/structure/fedora_resource_type/{fedora_resource_type}/delete",
@ -55,18 +54,4 @@ class FedoraResourceType extends ConfigEntityBundleBase implements FedoraResourc
*/ */
protected $label; protected $label;
/**
* The RDF type
*
* @var string
*/
protected $rdf_type;
/**
* {@inheritdoc}
*/
public function getRdfType()
{
return $this->rdf_type;
}
} }

1
src/FedoraResourceAccessControlHandler.php

@ -11,6 +11,7 @@ use Drupal\Core\Access\AccessResult;
* Access controller for the Fedora resource entity. * Access controller for the Fedora resource entity.
* *
* @see \Drupal\islandora\Entity\FedoraResource. * @see \Drupal\islandora\Entity\FedoraResource.
* @ingroup islandora
*/ */
class FedoraResourceAccessControlHandler extends EntityAccessControlHandler { class FedoraResourceAccessControlHandler extends EntityAccessControlHandler {

37
src/FedoraResourceHtmlRouteProvider.php

@ -2,8 +2,10 @@
namespace Drupal\islandora; namespace Drupal\islandora;
use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider; use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Drupal\Core\Entity\Routing\EntityRouteProviderInterface;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
/** /**
@ -11,8 +13,9 @@ use Symfony\Component\Routing\Route;
* *
* @see Drupal\Core\Entity\Routing\AdminHtmlRouteProvider * @see Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider * @see Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
* @ingroup islandora
*/ */
class FedoraResourceHtmlRouteProvider extends AdminHtmlRouteProvider { class FedoraResourceHtmlRouteProvider extends AdminHtmlRouteProvider implements EntityRouteProviderInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -37,9 +40,41 @@ class FedoraResourceHtmlRouteProvider extends AdminHtmlRouteProvider {
$collection->add("$entity_type_id.settings", $settings_form_route); $collection->add("$entity_type_id.settings", $settings_form_route);
} }
if ($uuid_route = $this->getUuidRoute($entity_type)) {
$collection->add("islandora.{$entity_type_id}.uuid", $uuid_route);
}
return $collection; return $collection;
} }
/** Gets the UUID route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getUuidRoute(EntityTypeInterface $entity_type) {
if ($entity_type->getKey('uuid') && $entity_type->hasViewBuilderClass() && $entity_type->hasLinkTemplate('uuid')) {
$entity_type_id = $entity_type->id();
$route = new Route($entity_type->getLinkTemplate('uuid'));
$route
->addDefaults([
'_entity_view' => 'fedora_resource.full',
'_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
])
->setRequirement('_entity_access', $entity_type_id . '.view')
->setOption('parameters', [
$entity_type_id => ['type' => 'entity:' . $entity_type_id],
])
// Fetch UUID pattern from Uuid class(constant)
->setRequirement($entity_type_id, '^' . Uuid::VALID_PATTERN . '$');
return $route;
}
}
/** /**
* Gets the collection route. * Gets the collection route.
* *

1
src/FedoraResourceTypeHtmlRouteProvider.php

@ -11,6 +11,7 @@ use Symfony\Component\Routing\Route;
* *
* @see Drupal\Core\Entity\Routing\AdminHtmlRouteProvider * @see Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider * @see Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
* @ingroup islandora
*/ */
class FedoraResourceTypeHtmlRouteProvider extends AdminHtmlRouteProvider { class FedoraResourceTypeHtmlRouteProvider extends AdminHtmlRouteProvider {

5
src/FedoraResourceTypeInterface.php

@ -10,9 +10,4 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface;
interface FedoraResourceTypeInterface extends ConfigEntityInterface { interface FedoraResourceTypeInterface extends ConfigEntityInterface {
// Add get/set methods for your configuration properties here. // Add get/set methods for your configuration properties here.
/**
* Return the entities rdf_type
*/
public function getRdfType();
} }

2
src/FedoraResourceTypeListBuilder.php

@ -16,7 +16,6 @@ class FedoraResourceTypeListBuilder extends ConfigEntityListBuilder {
public function buildHeader() { public function buildHeader() {
$header['label'] = $this->t('Fedora resource type'); $header['label'] = $this->t('Fedora resource type');
$header['id'] = $this->t('Machine name'); $header['id'] = $this->t('Machine name');
$header['rdf_type'] = $this->t('RDF Type');
return $header + parent::buildHeader(); return $header + parent::buildHeader();
} }
@ -26,7 +25,6 @@ class FedoraResourceTypeListBuilder extends ConfigEntityListBuilder {
public function buildRow(EntityInterface $entity) { public function buildRow(EntityInterface $entity) {
$row['label'] = $entity->label(); $row['label'] = $entity->label();
$row['id'] = $entity->id(); $row['id'] = $entity->id();
$row['rdf_type'] = $entity->getRdfType();
// You probably want a few more properties here... // You probably want a few more properties here...
return $row + parent::buildRow($entity); return $row + parent::buildRow($entity);
} }

26
src/FedoraResourceViewBuilder.php

@ -0,0 +1,26 @@
<?php
/**
* Created by PhpStorm.
* User: dpino
* Date: 8/16/16
* Time: 11:49 PM
*/
namespace Drupal\islandora;
use Drupal\node\NodeViewBuilder;
/**
* Class FedoraResourceViewBuilder
* view builder handler.
*
* @TODO this should manage how fedora_resource are build to be viewed.
*
* Right now we are just using Drupal\Core\Entity\EntityViewBuilder
* @ingroup islandora
*/
class FedoraResourceViewBuilder extends NodeViewBuilder {
}

279
src/Form/FedoraResourceEntityInlineForm.php

@ -0,0 +1,279 @@
<?php
namespace Drupal\islandora\Form;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\inline_entity_form\Form\EntityInlineForm;
use Drupal\inline_entity_form\InlineFormInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Generic entity inline form handler.
*/
class FedoraResourceEntityInlineForm extends EntityInlineForm implements InlineFormInterface {
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity type managed by this handler.
*
* @var \Drupal\Core\Entity\EntityTypeInterface
*/
protected $entityType;
/**
* Module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs the inline entity form controller.
*
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*/
public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, EntityTypeInterface $entity_type) {
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->entityType = $entity_type;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$container->get('entity_field.manager'),
$container->get('entity_type.manager'),
$container->get('module_handler'),
$entity_type
);
}
/**
* {@inheritdoc}
*/
public function getEntityType() {
return $this->entityType;
}
/**
* {@inheritdoc}
*/
public function getEntityTypeLabels() {
$lowercase_label = $this->entityType->getLowercaseLabel();
return [
'singular' => $lowercase_label,
'plural' => t('@entity_type entities', ['@entity_type' => $lowercase_label]),
];
}
/**
* {@inheritdoc}
*/
public function getEntityLabel(EntityInterface $entity) {
return $entity->label();
}
/**
* {@inheritdoc}
*/
public function getTableFields($bundles) {
$definitions = $this->entityFieldManager->getBaseFieldDefinitions($this->entityType->id());
$label_key = $this->entityType->getKey('label');
$label_field_label = t('Label');
if ($label_key && isset($definitions[$label_key])) {
$label_field_label = $definitions[$label_key]->getLabel();
}
$bundle_key = $this->entityType->getKey('bundle');
$bundle_field_label = t('Type');
if ($bundle_key && isset($definitions[$bundle_key])) {
$bundle_field_label = $definitions[$bundle_key]->getLabel();
}
$fields = [];
$fields['label'] = [
'type' => 'label',
'label' => $label_field_label,
'weight' => 1,
];
if (count($bundles) > 1) {
$fields[$bundle_key] = [
'type' => 'field',
'label' => $bundle_field_label,
'weight' => 2,
'display_options' => [
'type' => 'entity_reference_label',
'settings' => ['link' => FALSE],
],
];
}
return $fields;
}
/**
* {@inheritdoc}
*/
public function entityForm(array $entity_form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $entity_form['#entity'];
$form_display = $this->getFormDisplay($entity, $entity_form['#form_mode']);
$form_display->buildForm($entity, $entity_form, $form_state);
$entity_form['#ief_element_submit'][] = [get_class($this), 'submitCleanFormState'];
// Allow other modules to alter the form.
$this->moduleHandler->alter('inline_entity_form_entity_form', $entity_form, $form_state);
return $entity_form;
}
/**
* {@inheritdoc}
*/
public function entityFormValidate(array &$entity_form, FormStateInterface $form_state) {
// Perform entity validation only if the inline form was submitted,
// skipping other requests such as file uploads.
$triggering_element = $form_state->getTriggeringElement();
if (!empty($triggering_element['#ief_submit_trigger'])) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $entity_form['#entity'];
$this->buildEntity($entity_form, $entity, $form_state);
$form_display = $this->getFormDisplay($entity, $entity_form['#form_mode']);
$form_display->validateFormValues($entity, $entity_form, $form_state);
$entity->setValidationRequired(FALSE);
foreach($form_state->getErrors() as $name => $message) {
// $name may be unknown in $form_state and
// $form_state->setErrorByName($name, $message) may suppress the error message.
$form_state->setError($triggering_element, $message);
}
}
}
/**
* {@inheritdoc}
*/
public function entityFormSubmit(array &$entity_form, FormStateInterface $form_state) {
$form_state->cleanValues();
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $entity_form['#entity'];
$this->buildEntity($entity_form, $entity, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(EntityInterface $entity) {
$entity->save();
}
/**
* {@inheritdoc}
*/
public function delete($ids, $context) {
$storage_handler = $this->entityTypeManager->getStorage($this->entityType->id());
$entities = $storage_handler->loadMultiple($ids);
$storage_handler->delete($entities);
}
/**
* Builds an updated entity object based upon the submitted form values.
*
* @param array $entity_form
* The entity form.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function buildEntity(array $entity_form, ContentEntityInterface $entity, FormStateInterface $form_state) {
$form_display = $this->getFormDisplay($entity, $entity_form['#form_mode']);
$form_display->extractFormValues($entity, $entity_form, $form_state);
// Invoke all specified builders for copying form values to entity fields.
if (isset($entity_form['#entity_builders'])) {
foreach ($entity_form['#entity_builders'] as $function) {
call_user_func_array($function, [$entity->getEntityTypeId(), $entity, &$entity_form, &$form_state]);
}
}
}
/**
* Cleans up the form state for a submitted entity form.
*
* After field_attach_submit() has run and the form has been closed, the form
* state still contains field data in $form_state->get('field'). Unless that
* data is removed, the next form with the same #parents (reopened add form,
* for example) will contain data (i.e. uploaded files) from the previous form.
*
* @param $entity_form
* The entity form.
* @param $form_state
* The form state of the parent form.
*/
public static function submitCleanFormState(&$entity_form, FormStateInterface $form_state) {
$info = \Drupal::entityTypeManager()->getDefinition($entity_form['#entity_type']);
if (!$info->get('field_ui_base_route')) {
// The entity type is not fieldable, nothing to cleanup.
return;
}
$bundle = $entity_form['#entity']->bundle();
$instances = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_form['#entity_type'], $bundle);
foreach ($instances as $instance) {
$field_name = $instance->getName();
if (!empty($entity_form[$field_name]['#parents'])) {
$parents = $entity_form[$field_name]['#parents'];
array_pop($parents);
if (!empty($parents)) {
$field_state = array();
WidgetBase::setWidgetState($parents, $field_name, $form_state, $field_state);
}
}
}
}
/**
* Gets the form display for the given entity.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity.
* @param string $form_mode
* The form mode.
*
* @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface
* The form display.
*/
protected function getFormDisplay(ContentEntityInterface $entity, $form_mode) {
return EntityFormDisplay::collectRenderDisplay($entity, $form_mode);
}
}

9
src/Form/FedoraResourceTypeForm.php

@ -37,15 +37,6 @@ class FedoraResourceTypeForm extends EntityForm {
'#disabled' => !$fedora_resource_type->isNew(), '#disabled' => !$fedora_resource_type->isNew(),
); );
$form['rdf_type'] = array(
'#type' => 'textfield',
'#title' => $this->t('RDF Type'),
'#maxlength' => 255,
'#default_value' => $fedora_resource_type->getRdfType(),
'#description' => $this->t('Base RDF type for this entity'),
'#required' => TRUE,
);
/* You will need additional form elements for your custom properties. */ /* You will need additional form elements for your custom properties. */
return $form; return $form;

69
src/ParamConverter/UuidEntityConverter.php

@ -0,0 +1,69 @@
<?php
/**
* @file
* Contains UuidEntityConverter.php
*
* This file is part of the Islandora Project.
*
* (c) Islandora Foundation
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Diego Pino Navarro <dpino@metro.org> https://github.com/diegopino
*/
namespace Drupal\islandora\ParamConverter;
use Drupal\Core\ParamConverter\EntityConverter;
use Drupal\Core\ParamConverter\ParamConverterInterface;
use Drupal\Component\Uuid\Uuid;
use Symfony\Component\Routing\Route;
/**
* Converts an UUID param (route) into an Entity.
*
* @ingroup islandora
*/
class UuidEntityConverter extends EntityConverter implements ParamConverterInterface {
/**
* @inheritDoc
*/
public function convert($value, $definition, $name, array $defaults) {
$entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
if ($storage = $this->entityManager->getStorage($entity_type_id)) {
// Try to load fedora entity by UUID or ID depending on $value.
if (!(is_int($value) || ctype_digit((string) $value)) && Uuid::isValid($value)) {
$entities = $storage->loadByProperties(['uuid' => $value]);
$entity = ($entities) ? reset($entities) : NULL;
}
else {
$entity = parent::convert($value, $definition, $name, $defaults);
}
}
return $entity;
}
/**
* @inheritDoc
*/
public function applies($definition, $name, Route $route) {
/*if (!empty($definition['type']) && strpos($definition['type'], 'entity:') === 0) {
$entity_type_id = substr($definition['type'], strlen('entity:'));
if (strpos($definition['type'], '{') !== FALSE) {
$entity_type_slug = substr($entity_type_id, 1, -1);
return $name != $entity_type_slug && in_array($entity_type_slug, $route->compile()->getVariables(), TRUE);
}
return $this->entityManager->hasDefinition($entity_type_id);
}
return FALSE;*/
return parent::applies($definition, $name, $route); // TODO: Change the autogenerated stub
}
}
Loading…
Cancel
Save