Skip to content

Commit d178acd

Browse files
committed
Add listController, forms and settings options to content entity
1 parent db7876b commit d178acd

8 files changed

Lines changed: 468 additions & 1 deletion

src/Command/GeneratorEntityCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
8080
if (!$class) {
8181
$class = $dialog->ask(
8282
$output,
83-
$dialog->getQuestion('Enter the class of the entity', 'ConfigEntityType'),
83+
$dialog->getQuestion('Enter the class of the entity', 'ContentEntityType'),
8484
$class
8585
);
8686
}

src/Generator/EntityGenerator.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,43 @@ public function generate($module, $entity, $class)
109109
$this->getSourcePath($module).'/'.ucwords($entity).'Interface.php',
110110
$parameters
111111
);
112+
113+
$this->renderFile(
114+
'module/src/accessController-content-entity.php.twig',
115+
$this->getSourcePath($module).'/'.ucwords($entity).'AccessController.php',
116+
$parameters
117+
);
118+
119+
$this->renderFile(
120+
'module/src/Entity/content-entity.php.twig',
121+
$this->getEntityPath($module).'/'.ucwords($entity).'.php',
122+
$parameters
123+
);
124+
125+
$this->renderFile(
126+
'module/src/Entity/Controller/listController-content-entity.php.twig',
127+
$this->getEntityPath($module).'/Controller/'.ucwords($entity).'ListController.php',
128+
$parameters
129+
);
130+
131+
$this->renderFile(
132+
'module/src/Entity/Form/content-entity-settingsForm.php.twig',
133+
$this->getEntityPath($module).'/Form/'.ucwords($entity).'SettingsForm.php',
134+
$parameters
135+
);
136+
/*
137+
$this->renderFile(
138+
'module/src/Entity/Form/content-entity-form.php.twig',
139+
$this->getEntityPath($module).'/Form/'.ucwords($entity).'Form.php',
140+
$parameters
141+
);
142+
143+
$this->renderFile(
144+
'module/src/Entity/Form/content-entity-deleteForm.php.twig',
145+
$this->getEntityPath($module).'/Form/'.ucwords($entity).'deleteForm.php',
146+
$parameters
147+
);
148+
*/
112149
}
113150

114151
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\{{module}}\Entity\Controller\{{ entity|capitalize }}Controller.
6+
*/
7+
8+
namespace Drupal\{{module}}\Entity\Controller;
9+
10+
use Drupal\Core\Entity\EntityInterface;
11+
use Drupal\Core\Entity\EntityListBuilder;
12+
13+
/**
14+
* Provides a list controller for {{ entity|capitalize }} entity.
15+
*
16+
* @ingroup {{ entity|capitalize }}
17+
*/
18+
class {{ entity|capitalize }}ListController extends EntityListBuilder {
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function buildHeader() {
24+
$header['id'] = t('{{ entity|capitalize }}ID');
25+
$header['name'] = t('Name');
26+
return $header + parent::buildHeader();
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function buildRow(EntityInterface $entity) {
33+
/* @var $entity \Drupal\{{module}}\Entity\{{ entity|capitalize }} */
34+
$row['id'] = $entity->id();
35+
$row['name'] = \Drupal::l($this->getLabel($entity),
36+
'{{module}}.{{ entity|capitalize }}', array(
37+
'{{module}}_{{ entity|capitalize }}' => $entity->id(),
38+
));
39+
return $row + parent::buildRow($entity);
40+
}
41+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
/**
3+
* @file
4+
* Contains \Drupal\{{ module }}\Entity\{{ entity|capitalize }}.
5+
*/
6+
7+
namespace Drupal\{{ module }}\Entity;
8+
9+
use Drupal\Core\Entity\EntityStorageInterface;
10+
use Drupal\Core\Field\FieldDefinition;
11+
use Drupal\Core\Entity\ContentEntityBase;
12+
use Drupal\Core\Entity\EntityTypeInterface;
13+
use Drupal\{{ module }}\{{ entity|capitalize }}Interface;
14+
use Drupal\user\UserInterface;
15+
/**
16+
* Defines the {{ entity|capitalize }} entity.
17+
*
18+
* @ingroup {{ module }}
19+
*
20+
* @ContentEntityType(
21+
* id = "{{ entity|capitalize }}",
22+
* label = @Translation("{{ entity|capitalize }} entity"),
23+
* controllers = {
24+
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
25+
* "list_builder" = "Drupal\{{ module }}\Entity\Controller\{{ entity|capitalize }}ListController",
26+
*
27+
* "form" = {
28+
* "add" = "Drupal\{{ module }}\Form\{{ entity|capitalize }}Form",
29+
* "edit" = "Drupal\{{ module }}\Form\{{ entity|capitalize }}Form",
30+
* "delete" = "Drupal\{{ module }}\Form\{{ entity|capitalize }}DeleteForm",
31+
* },
32+
* "access" = "Drupal\{{ module }}\{{ entity|capitalize }}AccessController",
33+
* },
34+
* base_table = "{{ entity|capitalize }}",
35+
* admin_permission = "administer {{ entity|capitalize }} entity",
36+
* fieldable = TRUE,
37+
* entity_keys = {
38+
* "id" = "id{{ entity|capitalize }}",
39+
* "label" = "name",
40+
* "uuid" = "uuid"
41+
* },
42+
* links = {
43+
* "edit-form" = "{{ entity|capitalize }}.edit",
44+
* "admin-form" = "{{ entity|capitalize }}.settings",
45+
* "delete-form" = "{{ entity|capitalize }}.delete"
46+
* }
47+
* )
48+
*/
49+
class {{ entity|capitalize }} extends ContentEntityBase implements {{ entity|capitalize }}Interface {
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
55+
parent::preCreate($storage_controller, $values);
56+
$values += array(
57+
'user_id' => \Drupal::currentUser()->id(),
58+
);
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
public function getCreatedTime() {
65+
return $this->get('created')->value;
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
public function getChangedTime() {
72+
return $this->get('changed')->value;
73+
}
74+
75+
/**
76+
* {@inheritdoc}
77+
*/
78+
public function getOwner() {
79+
return $this->get('user_id')->entity;
80+
}
81+
82+
/**
83+
* {@inheritdoc}
84+
*/
85+
public function getOwnerId() {
86+
return $this->get('user_id')->target_id;
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public function setOwnerId($uid) {
93+
$this->set('user_id', $uid);
94+
return $this;
95+
}
96+
97+
/**
98+
* {@inheritdoc}
99+
*/
100+
public function setOwner(UserInterface $account) {
101+
$this->set('user_id', $account->id());
102+
return $this;
103+
}
104+
105+
/**
106+
* {@inheritdoc}
107+
*/
108+
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
109+
$fields['ida'] = FieldDefinition::create('integer')
110+
->setLabel(t('ID'))
111+
->setDescription(t('The ID of the {{ entity|capitalize }} entity.'))
112+
->setReadOnly(TRUE);
113+
114+
$fields['uuid'] = FieldDefinition::create('uuid')
115+
->setLabel(t('UUID'))
116+
->setDescription(t('The UUID of the {{ entity|capitalize }} entity.'))
117+
->setReadOnly(TRUE);
118+
119+
120+
$fields['name'] = FieldDefinition::create('string')
121+
->setLabel(t('Name'))
122+
->setDescription(t('The name of the {{ entity|capitalize }} entity.'))
123+
->setSettings(array(
124+
'default_value' => '',
125+
'max_length' => 50,
126+
'text_processing' => 0,
127+
))
128+
->setDisplayOptions('view', array(
129+
'label' => 'above',
130+
'type' => 'string',
131+
'weight' => -4,
132+
))
133+
->setDisplayOptions('form', array(
134+
'type' => 'string',
135+
'weight' => -4,
136+
))
137+
->setDisplayConfigurable('form', TRUE)
138+
->setDisplayConfigurable('view', TRUE);
139+
140+
$fields['user_id'] = FieldDefinition::create('entity_reference')
141+
->setLabel(t('User Name'))
142+
->setDescription(t('The Name of the associated user.'))
143+
->setSetting('target_type', 'user')
144+
->setSetting('handler', 'default')
145+
->setDisplayOptions('view', array(
146+
'label' => 'above',
147+
'type' => 'entity_reference',
148+
'weight' => -3,
149+
))
150+
->setDisplayOptions('form', array(
151+
'type' => 'entity_reference_autocomplete',
152+
'settings' => array(
153+
'match_operator' => 'CONTAINS',
154+
'size' => 60,
155+
'autocomplete_type' => 'tags',
156+
'placeholder' => '',
157+
),
158+
'weight' => -3,
159+
))
160+
->setDisplayConfigurable('form', TRUE)
161+
->setDisplayConfigurable('view', TRUE);
162+
163+
$fields['langcode'] = FieldDefinition::create('language')
164+
->setLabel(t('Language code'))
165+
->setDescription(t('The language code of {{ entity|capitalize }} entity.'));
166+
167+
$fields['created'] = FieldDefinition::create('created')
168+
->setLabel(t('Created'))
169+
->setDescription(t('The time that the entity was created.'));
170+
171+
$fields['changed'] = FieldDefinition::create('changed')
172+
->setLabel(t('Changed'))
173+
->setDescription(t('The time that the entity was last edited.'));
174+
175+
return $fields;
176+
}
177+
}
178+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\{{module}}\Entity\Form\{{ entity|capitalize }}DeleteForm.
6+
*/
7+
8+
namespace Drupal\{{module}}\Form;
9+
10+
use Drupal\Core\Entity\{{ entity|capitalize }}EntityConfirmFormBase;
11+
use Drupal\Core\Url;
12+
13+
/**
14+
* Provides a form for deleting a content_entity_example entity.
15+
*
16+
* @ingroup {{module}}
17+
*/
18+
class {{ entity|capitalize }}DeleteForm extends ContentEntityConfirmFormBase {
19+
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function getQuestion() {
25+
return t('Are you sure you want to delete entity %name?', array('%name' => $this->entity->label()));
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function getCancelRoute() {
32+
return new Url('{{module}}.contact_list');
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function getConfirmText() {
39+
return t('Delete');
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function submit(array $form, array &$form_state) {
46+
$this->entity->delete();
47+
48+
watchdog('content', '@type: deleted %title.', array('@type' => $this->entity->bundle(), '%title' => $this->entity->label()));
49+
$form_state['redirect_route']['route_name'] = '{{module}}.{{ entity|capitalize }}_list';
50+
}
51+
52+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* @file
4+
* Definition of Drupal\{{module}}\Entity\Form\{{ entity|capitalize }}FormController.
5+
*/
6+
7+
namespace Drupal\{{module}}\Form;
8+
9+
use Drupal\Core\Entity\{{ entity|capitalize }}EntityForm;
10+
use Drupal\Core\Language\Language;
11+
12+
/**
13+
* Form controller for the {{ entity|capitalize }} entity edit forms.
14+
*
15+
* @ingroup {{module}}
16+
*/
17+
class {{ entity|capitalize }}Form extends ContentEntityForm {
18+
19+
/**
20+
* Overrides Drupal\Core\Entity\EntityFormController::buildForm().
21+
*/
22+
public function buildForm(array $form, array &$form_state) {
23+
/* @var $entity \Drupal\{{module}}\Entity\{{ entity|capitalize }} */
24+
$form = parent::buildForm($form, $form_state);
25+
$entity = $this->entity;
26+
27+
$form['langcode'] = array(
28+
'#title' => t('Language'),
29+
'#type' => 'language_select',
30+
'#default_value' => $entity->getUntranslated()->language()->id,
31+
'#languages' => Language::STATE_ALL,
32+
);
33+
34+
return $form;
35+
}
36+
37+
/**
38+
* Overrides \Drupal\Core\Entity\EntityFormController::submit().
39+
*/
40+
public function submit(array $form, array &$form_state) {
41+
// Build the entity object from the submitted values.
42+
$entity = parent::submit($form, $form_state);
43+
$form_state['redirect_route']['route_name'] = '{{module}}.{{ entity|capitalize }}';
44+
45+
return $entity;
46+
}
47+
48+
/**
49+
* Overrides Drupal\Core\Entity\EntityFormController::save().
50+
*/
51+
public function save(array $form, array &$form_state) {
52+
$entity = $this->entity;
53+
$entity->save();
54+
}
55+
}

0 commit comments

Comments
 (0)