Skip to content

Instantly share code, notes, and snippets.

@Berdir
Created May 7, 2014 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Berdir/75041aaa46f391e844ac to your computer and use it in GitHub Desktop.
Save Berdir/75041aaa46f391e844ac to your computer and use it in GitHub Desktop.
NodeTypeDevelGenerate
<?php
/**
* @file
* Contains \Drupal\devel_generate\Plugin\DevelGenerate\VocabularyDevelGenerate.
*/
namespace Drupal\devel_generate\Plugin\DevelGenerate;
use Drupal\devel_generate\DevelGenerateBase;
use Drupal\Core\Language\Language;
use Drupal\node\NodeTypeInterface;
/**
* Provides a Content Type DevelGenerate plugin.
*
* @DevelGenerate(
* id = "node_type",
* label = @Translation("Content types"),
* description = @Translation("Generate a given number of content types."),
* url = "node_type",
* permission = "administer devel_generate",
* settings = {
* "num" = 1,
* "title_length" = 12,
* "kill" = FALSE
* }
* )
*/
class NodeTypeDevelGenerate extends DevelGenerateBase {
public function settingsForm(array $form, array &$form_state) {
$form['num'] = array(
'#type' => 'number',
'#title' => t('Number of content types?'),
'#default_value' => $this->getSetting('num'),
'#size' => 10,
);
$form['title_length'] = array(
'#type' => 'textfield',
'#title' => t('Maximum number of characters for the title'),
'#default_value' => $this->getSetting('title_length'),
'#size' => 10,
);
$form['kill'] = array(
'#type' => 'checkbox',
'#title' => t('Delete existing content types before generating new ones.'),
'#default_value' => $this->getSetting('kill'),
);
$form['fields'] = array(
'#type' => 'details',
'#title' => t('Fields'),
);
$form['fields']['fields_num'] = array(
'#type' => 'number',
'#title' => t('Amount of fields to create'),
'#default_value' => 0,
);
return $form;
}
/**
* {@inheritdoc}
*/
public function generateElements(array $values) {
if ($values['kill']) {
$this->deleteNodeTypes();
$this->setMessage(t('Deleted existing content types.'));
}
$new_types = $this->generateNodeTypes($values['num'], $values['title_length'], $values['fields_num']);
if (!empty($new_types)) {
$this->setMessage(t('Created the following new content types: !node_types', array('!node_types' => implode(', ', $new_types))));
}
}
/**
* Deletes all vocabularies.
*/
protected function deleteNodeTypes() {
foreach (entity_load_multiple('node_type') as $node_type) {
$node_type->delete();
}
}
function generateNodeTypes($records, $maxlength = 12, $fields_num) {
$node_types = array();
// Insert new data:
for ($i = 1; $i <= $records; $i++) {
$name = $this->generateWord(mt_rand(2, $maxlength));
$node_type = entity_create('node_type', array(
'name' => $name,
'type' => str_replace(' ', '_', drupal_strtolower($name)),
'description' => "description of $name",
));
$node_type->save();
$node_types[] = $node_type->label();
for ($field_index = 1; $field_index <= $fields_num; $field_index++) {
$this->createField($maxlength, $node_type);
}
unset($node_type);
}
return $node_types;
}
public function validateDrushParams($args) {
$values = array(
'num' => array_shift($args),
'kill' => drush_get_option('kill'),
'title_length' => 12,
);
if ($this->isNumber($values['num']) == FALSE) {
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of vocabularies: !num.', array('!num' => $values['num'])));
}
return $values;
}
/**
* @param $maxlength
* @param $field
* @param $instance
* @param $node_type
*/
protected function createField($maxlength, NodeTypeInterface $node_type) {
$label = $this->generateWord(mt_rand(2, $maxlength));
$field_name = str_replace(' ', '_', drupal_strtolower($label));
if (empty($field)) {
$field = entity_create('field_config', array(
'name' => $field_name,
'entity_type' => 'node',
'type' => 'text',
));
$field->save();
}
if (empty($instance)) {
$instance = entity_create('field_instance_config', array(
'field_name' => $field_name,
'entity_type' => 'node',
'bundle' => $node_type->id(),
'label' => $label,
));
$instance->save();
// Assign widget settings for the 'default' form mode.
entity_get_form_display('node', $node_type->id(), 'default')
->setComponent($field_name, array(
'type' => 'text_textfield',
))
->save();
// Assign display settings for the 'default' and 'teaser' view modes.
entity_get_display('node', $node_type->id(), 'default')
->setComponent($field_name, array(
'label' => 'hidden',
'type' => 'text_default',
))
->save();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment