Skip to content

Commit

Permalink
add token support
Browse files Browse the repository at this point in the history
  • Loading branch information
aviggngyv committed Feb 5, 2018
1 parent a853915 commit 24e7cdc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
26 changes: 22 additions & 4 deletions core/command/ContentTypeCreateCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,17 @@ protected function configure() {
'commands.create.content-type.options.description'
)
->addOption(
'entity_support',
'',
InputOption::VALUE_REQUIRED,
'commands.create.content-type.options.entity_support'
'entity_support',
'',
InputOption::VALUE_REQUIRED,
'commands.create.content-type.options.entity_support'
)
->addOption(
'token_support',
'',
InputOption::VALUE_REQUIRED,
'commands.create.content-type.options.token_support'
)
->addOption(
'fields',
'',
Expand All @@ -113,11 +119,13 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$lable_name = $ct_cache['lable_name'];
$description = $ct_cache['description'];
$entity_support = (bool) $ct_cache['entity_support'];
$token_support = (bool) $ct_cache['token_support'];
$fields = $ct_cache['fields'];
}else {
$lable_name = $input->getOption('lable_name');
$description = $input->getOption('description');
$entity_support = (bool) $input->getOption('entity_support');
$token_support = (bool) $input->getOption('token_support');
$fields = $input->getOption('fields');
}

Expand All @@ -138,6 +146,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
'--create-composer' => FALSE,
'isContentType' => TRUE,
'supportEntity' => $entity_support,
'supportToken' => $token_support,
'fields' => $fields,
);

Expand Down Expand Up @@ -286,6 +295,14 @@ protected function interact(InputInterface $input, OutputInterface $output) {
$input->setOption('entity_support', $entity_support);
}

// --support token option
$token_support = $input->getOption('token_support');
if (!$token_support) {
$token_support_question = new ConfirmationQuestion('Enable support Token (y/n) [No]? ', FALSE);
$token_support = $helper->ask($input, $output, $token_support_question);
$input->setOption('token_support', $token_support);
}

// --fields option
$fields = $input->getOption('fields');
if (!$fields) {
Expand Down Expand Up @@ -445,6 +462,7 @@ protected function interact(InputInterface $input, OutputInterface $output) {
'lable_name' => $lable_name,
'description' => $description,
'entity_support' => $entity_support,
'token_support' => $token_support,
'fields' => $fields
);

Expand Down
7 changes: 7 additions & 0 deletions core/command/ModuleCreateCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ protected function configure() {
InputArgument::OPTIONAL,
'Is this suppport entity?'
)
->addArgument(
'supportToken',
InputArgument::OPTIONAL,
'Is this suppport token?'
)
->addArgument(
'fields',
InputArgument::OPTIONAL,
Expand All @@ -143,6 +148,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$create_composer = $input->getOption('create-composer');
$isContentType = $input->getArgument('isContentType');
$supportEntity = $input->getArgument('supportEntity');
$supportToken = $input->getArgument('supportToken');
$fields = $input->getArgument('fields');

$dir .= '/'.$machineName;
Expand Down Expand Up @@ -184,6 +190,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
'dependencies' => $dependencies,
'isContentType' => $isContentType,
'supportEntity' => $supportEntity,
'supportToken' => $supportToken,
'fields' => $fields
);

Expand Down
39 changes: 39 additions & 0 deletions core/command/templates/module.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,45 @@
}
<?php endif; ?>

<?php if($supportToken): ?>
/**
* Implements hook_tokens().
*/
function <?php print $machine_name; ?>_tokens($type, $tokens, array $data = [], array $options = []) {
$replacements = array();
if ($type == '<?php print $machine_name; ?>') {
$<?php print $machine_name; ?> = reset($data);
foreach ($tokens as $name => $original) {
switch ($name) {
case '<?php print substr($machine_name, 0, 1 ); ?>id':
$replacements[$original] = $<?php print $machine_name; ?>-><?php print substr($machine_name, 0, 1 ); ?>id;
break;

<?php foreach ($fields as $field): ?>
case '<?php print $field['name']; ?>':
$replacements[$original] = $<?php print $machine_name; ?>-><?php print $field['name']; ?>;
break;
<?php endforeach; ?>

case 'uid':
$replacements[$original] = $<?php print $machine_name; ?>->uid;
break;

case 'created':
$replacements[$original] = date("Y-m-d H:i:s", $<?php print $machine_name; ?>->created);
break;

case 'updated':
$replacements[$original] = date("Y-m-d H:i:s", $<?php print $machine_name; ?>->updated);
break;
}
}
}

return $replacements;
}
<?php endif; ?>

/**
* get <?php print $module; ?> by id
*/
Expand Down

0 comments on commit 24e7cdc

Please sign in to comment.