Drupal 7 programmatically create webform components - drupal-7

I've followed this great document which successfully creates a webform and associated components.
I am trying to adjust the code so it works for existing webforms instead of creating new ones.
My code is as follows:
$nid = 12;
$node = node_load(12);
// Create the webform components.
$components = array(
array(
'name' => 'Gender',
'form_key' => 'gender',
'type' => 'select',
'mandatory' => 1,
'weight' => 0,
'pid' => 0,
'extra' => array(
'title_display' => 'inline',
'private' => 0,
'items' => "Mrs|Mrs\nMiss|Miss\nMr|Mr",
'aslist' => 1,
),
),
);
// Setup notification email.
$emails = array(
array(
'email' => 'somebody#example.tld',
'subject' => 'default',
'from_name' => 'default',
'from_address' => 'default',
'template' => 'default',
'excluded_components' => array(),
),
);
// Attach the webform to the node.
$node->webform = array(
'confirmation' => '',
'confirmation_format' => NULL,
'redirect_url' => '<confirmation>',
'status' => '1',
'block' => '0',
'teaser' => '0',
'allow_draft' => '0',
'auto_save' => '0',
'submit_notice' => '1',
'submit_text' => '',
'submit_limit' => '-1', // User can submit more than once.
'submit_interval' => '-1',
'total_submit_limit' => '-1',
'total_submit_interval' => '-1',
'record_exists' => TRUE,
'roles' => array(
0 => '1', // Anonymous user can submit this webform.
),
'emails' => $emails,
'components' => $components,
);
// Save the node.
node_save($node);
When I attempt to execute my code I get the following error:
Error message SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'nid' cannot be null

First try to print all the available values of $node.
echo "<pre>";
print_r($node);
echo "</pre>"'
exit;
check that you are getting value for $node[nid] or not.

Related

TYPO3 Custom Content fields on save database error

I added some content Fields which looks as it follows;
$temporaryColumnHC = array(
'my_type' => array(
'exclude' => 0,
'label' => 'My Type',
'config' => array(
'type' => 'select',
'renderType' => 'selectSingle',
'items' => array(
array('Bar', 'bar'),
array('Pie', 'pie'),
array('Donut', 'donut'),
array('Line', 'line'),
array('Bar 2', 'bar2'),
array('Bar 3', 'bar3'),
array('Bubble', 'bubble'),
)
)
),
'my_suffix' => array(
'exclude' => 0,
'label' => 'My Label Suffix',
'config' => array(
'type' => 'input',
'size' => 10,
'max' => 20
)
),
'my_source_url' => array(
'exclude' => 0,
'label' => 'Source URL',
'config' => array(
'type' => 'input',
'renderType' => 'inputLink'
)
),
'my_source' => array(
'exclude' => 0,
'label' => 'Source Text',
'config' => array(
'type' => 'text',
'cols' => 40,
'rows' => 15
)
),
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'tt_content',
$temporaryColumnHC
);
but on save I get the following error
error [1620]: Unknown column 'suffix' in 'field list'
What I have to do that this fields is saved in database
As mentioned already as a comment, TYPO3 complains that the column is missing in the Database.
Your extension should provide an ext_tables.sql which extends the Database Schema. See https://docs.typo3.org/m/typo3/reference-coreapi/10.4/en-us/ExtensionArchitecture/FilesAndLocations/Index.html#ext-tables-sql for further information.
The file always contains CREATE TABLE statements, which will be converted by TYPO3 do update the schema.
Updating the schema can be done through the install tool of TYPO3, or tools like TYPO3 console via command line.

How do I add publish dates to uploaded files in Typo3?

I am currently maintaining a client's existing Typo3 6.1 site and one of the things they would like to add to their site is the possibility of applying publish and expiration dates on uploaded files, in the same way such dates can be applied on Typo3 pages.
I did some research on the matter to see if something like this had already been done before. I tried looking for existing Typo3 extensions on Google and on the official Typo3 extensions repository that would add this functionality, but nothing like this seems to exist. I also tried to find ways of doing it myself, still without any results.
What would be the best approach to add this functionality to a Typo3 6.1 website?
EDIT
After some investigating, I noticed another extension was already hooked up with the TCEForm of the file editing page: http://i.stack.imgur.com/up7mO.png
In this screenshot, you can see I managed to add the start and end datetime fields as the first two fields in the form ("Date de publication" and "Date d'expiration"). I currently have the following configuration in my ext_table.php:
<?php
if (!defined('TYPO3_MODE')) {
die('Access denied.');
}
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin(
$_EXTKEY, 'Documents', 'Documents'
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile($_EXTKEY, 'Configuration/TypoScript', 'Documents');
\TYPO3\CMS\Core\Utility\GeneralUtility::loadTCA('sys_file');
$newFileColumns = array(
'date_publication' => array(
'exclude' => 0,
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.date_publication',
'config' => array(
'type' => 'input',
'size' => 13,
'max' => 20,
'eval' => 'datetime',
'checkbox' => 0,
'default' => 0,
'range' => array(
'lower' => mktime(0, 0, 0, date('m'), date('d'), date('Y'))
),
),
),
'date_expiration' => array(
'exclude' => 0,
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.date_expiration',
'config' => array(
'type' => 'input',
'size' => 13,
'max' => 20,
'eval' => 'datetime',
'checkbox' => 0,
'default' => 0,
'range' => array(
'lower' => mktime(0, 0, 0, date('m'), date('d'), date('Y'))
),
),
),
'desactive' => array(
'exclude' => 1,
'config' => array(
'type' => 'passthrough'
)
),
'isbn' => array(
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.isbn',
'config' => array(
'type' => 'input',
'size' => 50,
'eval' => 'trim'
),
),
'auteurs_internes' => array(
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.auteurs_internes',
'config' => array(
'type' => 'select',
'foreign_table' => 'fe_users',
'MM' => 'tx_documents_document_auteur_mm',
'size' => 5,
'maxitems' => 10,
'minitems' => 0,
'wizards' => array(
'suggest' => array(
'type' => 'suggest',
'default' => array(
'searchWholePhrase' => 1
),
),
),
),
),
'auteurs_externes' => array(
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.auteurs_externes',
'config' => array(
'type' => 'input',
'size' => 50,
'eval' => 'trim'
),
),
'responsable' => array(
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.responsable',
'config' => array(
'type' => 'group',
'internal_type' => 'db',
'allowed' => 'tx_common_domain_model_unitelogique',
'size' => 1,
'maxitems' => 1,
'minitems' => 0,
'wizards' => array(
'suggest' => array(
'type' => 'suggest',
'default' => array(
'searchWholePhrase' => 1
),
),
),
),
),
'type_document' => array(
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.type_document',
'config' => array(
'type' => 'select',
'foreign_table' => 'sys_category',
'foreign_table_where' => ' AND sys_category.tx_common_type = "type_document" ORDER BY sys_category.title ASC',
'maxitems' => 1,
'minitems' => 1,
'renderMode' => 'tree',
'treeConfig' => array(
'parentField' => 'parent',
),
),
),
'public_cible' => array(
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.public_cible',
'config' => array(
'type' => 'select',
'foreign_table' => 'sys_category',
'MM' => 'tx_documents_document_publiccible_mm',
'foreign_table_where' => ' AND sys_category.tx_common_type = "public_cible_document" ORDER BY sys_category.title ASC',
'maxitems' => 100,
'minitems' => 0,
'renderMode' => 'tree',
'treeConfig' => array(
'parentField' => 'parent',
),
),
),
'important' => array(
'exclude' => 1,
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.important',
'config' => array(
'type' => 'check',
),
),
'nouveau' => array(
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.nouveau',
'config' => array(
'type' => 'input',
'eval' => 'date'
),
),
'misajour' => array(
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.misajour',
'config' => array(
'type' => 'check',
),
),
'no_magistra' => array(
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.no_magistra',
'config' => array(
'type' => 'input',
'size' => 50,
'eval' => 'trim'
),
),
'centre_documentation' => array(
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.centre_documentation',
'config' => array(
'type' => 'check',
),
),
'image' => array(
'exclude' => 0,
'label' => 'LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.image',
'config' => array(
'type' => 'group',
'internal_type' => 'file',
'allowed' => $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
'minitems' => 0,
'maxitems' => 1,
'size' => 1,
'show_thumbs' => 1,
'uploadfolder' => 'uploads/pics',
'disable_controls' => 'list',
),
),
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('sys_file', $newFileColumns);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('sys_file', '--div--;LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:document.tab, date_publication, date_expiration, isbn, auteurs_internes, auteurs_externes, responsable, type_document, public_cible, important, nouveau, no_magistra, centre_documentation, image');
// Edit of existing fields
\TYPO3\CMS\Core\Utility\GeneralUtility::loadTCA('sys_file');
$TCA['sys_file']['columns']['title']['config']['placeholder'] = '';
$TCA['sys_file']['columns']['title']['config']['eval'] = 'required';
$TCA['sys_file']['columns']['title']['config']['size'] = '50';
$TCA['sys_file']['columns']['description']['config']['wizards'] = array(
'RTE' => array(
'icon' => 'wizard_rte2.gif',
'notNewRecords' => 1,
'RTEonly' => 1,
'script' => 'wizard_rte.php',
'title' => 'LLL:EXT:cms/locallang_ttc.xlf:bodytext.W.RTE',
'type' => 'script'
)
);
$TCA['sys_file']['columns']['description']['defaultExtras'] = 'richtext:rte_transform[flag=rte_enabled|mode=ts]';
// Add category types
\TYPO3\CMS\Core\Utility\GeneralUtility::loadTCA('sys_category');
$TCA['sys_category']['columns']['tx_common_type']['config']['items'][] = array('LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:category.type.type_document', 'type_document');
$TCA['sys_category']['columns']['tx_common_type']['config']['items'][] = array('LLL:EXT:the_extension_key/Resources/Private/Language/locallang_db.xlf:category.type.public_cible_document', 'public_cible_document');
?>
However, defining the columns as start and end access dates with
$GLOBALS['TCA']['sys_file']['ctrl']['enablecolumns']['starttime'] = 'tx_myext_starttime';
$GLOBALS['TCA']['sys_file']['ctrl']['enablecolumns']['endtime'] = 'tx_myext_starttime';
as suggested in the answers makes the file list show an error instead of loading a list of files. Instead, I tried using the $TCA variable to define them. The error page shown by Typo3 is not shown anymore using this, but the editing page still can't be rendered successfully.
The $TCA configuration I added:
$TCA['sys_file'] = array(
'ctrl' => array(
'label' => 'title',
'label_alt' => 'name,description,alternative,identifier,uid',
'tstamp' => 'tstamp',
'crdate' => 'crdate',
'cruser_id' => 'cruser_id',
'versioningWS' => 2,
'versioning_followPages' => TRUE,
'origUid' => 't3_origuid',
'enablecolumns' => array(
'disabled' => 'desactive',
'starttime' => 'date_publication',
'endtime' => 'date_expiration',
)
)
);
The following image shows what happens with this $TCA configuration added while trying to edit a file: http://i.stack.imgur.com/8VaCD.png
The TCEForm is simply not rendered and I don't know why. I'm not sure if my configuration in ext_tables.php contains a bad configuration (especially in the $TCA config I added) or if the problem comes from another file in the extension.
What can be causing this error and how can I fix it?
FAL (File Abstraction Layer) does not have a starttime and endtime field.
But since the sys_file related tables (like sys_file_reference) can be extended by your own extension and TCA offers you the starttime/endtime functionality by default my biggest guess will be that this can be accomplished with some modifications in the TCA.
Here a quick kickstart of how you can try to extend sys_file with the starttime and endtime fields.
ext_emconf.php
$EM_CONF[$_EXTKEY] = array (
// more stuff here in between
'constraints' => array (
'depends' => array (
'filelist' => '6.1.0-6.1.99',
),
),
);
ext_tables.sql
#
# Table structure for table 'sys_file'
#
CREATE TABLE sys_file (
tx_myext_starttime int(11) unsigned DEFAULT '0' NOT NULL,
tx_myext_endtime int(11) unsigned DEFAULT '0' NOT NULL,
)
ext_tables.php
<?php
if (!defined ('TYPO3_MODE')) {
die ('Access denied.');
}
$tempColumns = array();
$tempColumns['tx_myext_starttime'] = array(
'exclude' => 1,
'l10n_mode' => 'mergeIfNotBlank',
'label' => 'LLL:EXT:lang/locallang_general.xlf:LGL.starttime',
'config' => array(
'type' => 'input',
'size' => 13,
'max' => 20,
'eval' => 'datetime',
'checkbox' => 0,
'default' => 0,
'range' => array(
'lower' => mktime(0, 0, 0, date('m'), date('d'), date('Y'))
),
),
);
$tempColumns['tx_myext_endtime'] = array(
'exclude' => 1,
'l10n_mode' => 'mergeIfNotBlank',
'label' => 'LLL:EXT:lang/locallang_general.xlf:LGL.endtime',
'config' => array(
'type' => 'input',
'size' => 13,
'max' => 20,
'eval' => 'datetime',
'checkbox' => 0,
'default' => 0,
'range' => array(
'lower' => mktime(0, 0, 0, date('m'), date('d'), date('Y'))
),
),
);
$GLOBALS['TCA']['sys_file']['ctrl']['enablecolumns']['starttime'] = 'tx_myext_starttime';
$GLOBALS['TCA']['sys_file']['ctrl']['enablecolumns']['endtime'] = 'tx_myext_starttime';
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('sys_file', $tempColumns, 1);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('sys_file', '--div--;LLL:EXT:cms/locallang_ttc.xlf:tabs.access, tx_myext_starttime, tx_myext_endtime');
unset($tempColumns);
I don't know if the FAL persistence layer and repositories does support (or better: eat) this configuration and I can't estimate in which kind of problems you're gonna run. But I guess it's a try worth.
Good luck!

Drupal 7 Feeds Module not working

I have installed the Drupal 7 feeds module and created an Importer.
I can display the Stand-alone form, add a file but the upload / parse page just displays the message "Importing..." It doesn't do much else and does not import into a selected content type. No errors are displayed.
Any ideas?
// Importer below /////////////////////////////////////////////////////////
$feeds_importer = new stdClass();
$feeds_importer->disabled = FALSE;
$feeds_importer->api_version = 1;
$feeds_importer->id = 'shop_ids';
$feeds_importer->config = array(
'name' => 'Shop IDs',
'description' => 'Import a CSV file which feeds into Shop ID node type',
'fetcher' => array(
'plugin_key' => 'FeedsFileFetcher',
'config' => array(
'allowed_extensions' => 'txt csv tsv xml opml',
'direct' => 0,
'directory' => 'public://feeds',
'allowed_schemes' => array(
'public' => 'public',
'private' => 'private',
),
),
),
'parser' => array(
'plugin_key' => 'FeedsCSVParser',
'config' => array(
'delimiter' => ',',
'no_headers' => 1,
),
),
'processor' => array(
'plugin_key' => 'FeedsNodeProcessor',
'config' => array(
'expire' => '-1',
'author' => 0,
'authorize' => 1,
'mappings' => array(
0 => array(
'source' => '\'0\'',
'target' => 'title',
'unique' => 1,
),
1 => array(
'source' => '\'1\'',
'target' => 'body',
'unique' => FALSE,
),
),
'update_existing' => '1',
'input_format' => 'plain_text',
'skip_hash_check' => 0,
'bundle' => 'shop_ids',
),
),
'content_type' => '',
'update' => 0,
'import_period' => '-1',
'expire_period' => 3600,
'import_on_create' => 1,
'process_in_background' => 0,
);
Looks like there was an error with batch.js
JQuery causing function error
fix was to add ; to beginning of code.
;(function ($) {
and add the (jQuery); to the end of the file...
})(jQuery);

Cakephp find not returning the complete string

I have a model BudgetsProduct that is associated with the model Product, but it's returning the Product data cutting the first character on fields id and title, how you can see above.
Notice that BudgetsProduct.product_id is 7724 and its returning the correct associated product row, but trimming the first character like Product.id is returning just 724.
'BudgetsProduct' => array(
(int) 0 => array(
'id' => '3',
'budget_id' => '2',
'product_id' => '7724',
'quant' => '3.00',
'price' => '3175.00',
'discount' => '0.00',
'discount_type' => '%',
'product_options' => 'null',
'active' => '1',
'creator' => null,
'created' => '2014-08-27 15:52:35',
'modifier' => null,
'modified' => '2014-08-27 15:52:35',
'Product' => array(
'id' => '724',
'title' => 'onjunto Pina (03)',
'opt_title' => null,
'parent_id' => null,
'stock' => false,
'active' => '',
'creator' => null,
'created' => '013-12-11 10:53:30',
'modifier' => null,
'modified' => '014-04-09 15:44:24'
)
)

CakePHP find remove fields or recursive find

I'm trying to slim down my finds queries, by specifying which fields to use. I don't understand how I can specify specific fields for my HABTM fields.
This is my find:
$lastviewed = $this->Lastviewedproduct->find('all', array(
'fields' => array(
'Lastviewedproduct.id', 'Lastviewedproduct.sessionid', 'Lastviewedproduct.product_id',
'Product.id', 'Product.name', 'Product.slug', 'Product.price',
//'Category.id',
//'Mediafile.*',
),
'conditions' => array(
'Lastviewedproduct.status' => 'active',
'Lastviewedproduct.sessionid' => $this->Session->read('Companyname.sessionid'),
'Product.hidden_on_site' => 0,
'Product.visibility' => 1,
'Product.status' => 'active',
),
'order' => 'Lastviewedproduct.modified DESC',
'limit' => 5,
'recursive' => 2,
));
This is what it returns:
(int) 2 => array(
'Lastviewedproduct' => array(
'id' => '97',
'sessionid' => '14035312318244955',
'product_id' => '2'
),
'Product' => array(
'id' => '2',
'name' => 'blokhut 2',
'slug' => 'blokhut-2',
'price' => '200.00',
'Category' => array(
(int) 0 => array(
'id' => '218',
'zosomodule_id' => '25',
'user_add_id' => '0',
'user_update_id' => '0',
'created' => '2013-11-27 10:00:00',
'modified' => '2013-11-27 10:00:00',
'status' => 'active',
'visibility' => true,
'parent_id' => '2',
'shipment_id' => '1',
'name' => 'Metalen Tuinbergingen ',
'slug' => 'metalen-tuinbergingen-',
'pagetitle' => 'Metalen Tuinbergingen ',
'content' => '<p class="p1"><span class="s1">Metalen tuinbergingen zijn een praktische en kwalitatieve oplossing voor meer bergruimte in uw tuin. Wij hebben diverse merken metalen bergingen zoals </span><span class="s2">Biohort</span><span class="s1"> en </span><span class="s2">Yardmaster</span><span class="s1">. Hier hebben wij tevens ook tuinkasten en opbergboxen van.</span></p>
<p class="p1"><span class="s1"><br /></span></p>
<p class="p1"><span class="s1">Mocht u op zoek zijn naar andere varianten tuinbergingen, bekijk dan ons gehele assortiment </span><span class="s2">tuinbergingen.</span></p>',
'metatitle' => 'Metalen tuinbergingen',
'metadescription' => null,
'discountoffer' => false,
'discount_normal' => '0.00',
'discount_max' => '0.00',
'discount_ideal' => '0.00',
'cost_rembours' => '0.00',
'in_mainmenu' => true,
'hide_sidebar' => false,
'show_block' => true,
'sort_order' => '0',
'ProductsCategory' => array(
'id' => '2',
'created' => '2014-01-01 10:00:00',
'modified' => '2014-01-01 10:00:00',
'product_id' => '2',
'category_id' => '218'
)
)
),
'Mediafile' => array()
)
),
I don't want all these Category fields, but only Category.id, Category.name and Category.slug for example. How do i say that in my find? As you can see I tried adding Category.id in my fields array, but then I get an error. I read something about containable, but I didn't get that working and I don't know if that is the solution.
Thanks in advance for your help!
You can use ContainableBehavior for this task. You should write something like this:
1 attach behavior
permanent
class Lastviewedproduct extends AppModel {
public $actsAs = array('Containable');
....
}
on demand
$this->Lastviewedproduct->Behaviors->load('Containable');
2 specify fields
$this->Lastviewedproduct->find('all', array(
'contain' => array(
'Product' => array(
'Category' => array(
'fields' => array('id','name','slug')
),
),
'Mediafile'
)
));
Add field parameter with relation ship where you give relationship to Category MODEL
public $hasMany = array(
'Category' => array(
'className' => 'Category',
'fields' => array('Category.id','Category.name','Category.slug')
)
);
As Dimitry said above - Containable is the way to go with this:
In your Models/AppModel.php (Create it if it doesn't exist) change the call to have the following:
class AppModel extends Model {
public $actsAs = array('Containable');
}
Then you would do as Dimitry said above - this will allow you to only return the fields you want.
'recursive' => 2
will eventually cause you so much slowness that your app will be painful to use, most people set recursive = 1 in their AppModel and use containable the rest of the time.

Resources