Skip to content

Instantly share code, notes, and snippets.

@sreynen
Last active August 29, 2015 14:04
Show Gist options
  • Save sreynen/170c1c131ff3b4b1d246 to your computer and use it in GitHub Desktop.
Save sreynen/170c1c131ff3b4b1d246 to your computer and use it in GitHub Desktop.
Quick Drupal 8 Content Type Creator
<?php
/**
* Creates a tar file.
* Directly copied from features_tar_create().
*/
function create_tar($name, $contents) {
/* http://www.mkssoftware.com/docs/man4/tar.4.asp */
/* http://www.phpclasses.org/browse/file/21200.html */
$tar = '';
$bigheader = $header = '';
if (strlen($name) > 100) {
$bigheader = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12",
'././@LongLink', '0000000', '0000000', '0000000',
sprintf("%011o", strlen($name)), '00000000000',
' ', 'L', '', 'ustar ', '0',
'', '', '', '', '', '');
$bigheader .= str_pad($name, floor((strlen($name) + 512 - 1) / 512) * 512, "\0");
$checksum = 0;
for ($i = 0; $i < 512; $i++) {
$checksum += ord(substr($bigheader, $i, 1));
}
$bigheader = substr_replace($bigheader, sprintf("%06o", $checksum)."\0 ", 148, 8);
}
$header = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12", // book the memorie area
substr($name,0,100), // 0 100 File name
'100644 ', // File permissions
' 765 ', // UID,
' 765 ', // GID,
sprintf("%11s ", decoct(strlen($contents))), // Filesize,
sprintf("%11s", decoct(time())), // Creation time
' ', // 148 8 Check sum for header block
'', // 156 1 Link indicator / ustar Type flag
'', // 157 100 Name of linked file
'ustar ', // 257 6 USTAR indicator "ustar"
' ', // 263 2 USTAR version "00"
'', // 265 32 Owner user name
'', // 297 32 Owner group name
'', // 329 8 Device major number
'', // 337 8 Device minor number
'', // 345 155 Filename prefix
''); // 500 12 ??
$checksum = 0;
for ($i = 0; $i < 512; $i++) {
$checksum += ord(substr($header, $i, 1));
}
$header = substr_replace($header, sprintf("%06o", $checksum)."\0 ", 148, 8);
$tar = $bigheader.$header;
$buffer = str_split($contents, 512);
foreach ($buffer as $item) {
$tar .= pack("a512", $item);
}
return $tar;
}
if ($_POST['input']) {
$lines = explode("\n", $_POST['input']);
$filename = 'content_types.tar';
header('Content-type: application/x-tar');
header('Content-Disposition: attachment; filename="' . $filename . '"');
$info = implode("\n", array(
'name: Content Types',
'type: module',
"description: 'Content Types created from tab-delimited text (e.g. Google Sheets).'",
'version: 1.0',
'core: 8.x'
));
print create_tar('content_types/content_types.info.yml', $info);
foreach ($lines as $line) {
list($machine, $display, $description) = explode("\t", trim($line));
$type = implode("\n", array(
"name: '" . $display . "'",
'type: ' . $machine,
"description: '" . $description . "'"
));
print create_tar('content_types/config/install/node.type.' . $machine . '.yml', $type);
}
print pack('a1024', '');
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="charset" charset="utf-8" />
<title>Content Types</title>
</head>
<body>
<h3>Paste tab-delimited (e.g. copy-pasted from Google Sheets) machine name, display name, description to generate a module to create those content types.</h3>
<form action="content_types.php" method="post">
<textarea rows="10" cols="100" name="input"></textarea>
<input type="submit" value="Download Module" />
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment