PHP code example of teppokoivula / import-tool

1. Go to this page and download the library: Download teppokoivula/import-tool library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

teppokoivula / import-tool example snippets


$config->ImportTools = [
	'profiles' => [
		'members' => [
			'label' => 'Members',
			'template' => 'member',
			'parent' => 1282,
			'values' => [
				// note: key (here 'title') should be a reference to a column name/header from the import file
				'title' => [
					'field' => 'title',
					'sanitize' => 'text',
				],
				'first_name' => [
					'field' => 'first_name',
					'sanitize' => 'text',
				],
				'last_name' => [
					'field' => 'last_name',
					'sanitize' => 'text',
				],
				'date_of_birth' => [
					'field' => 'date_of_birth',
					'sanitize' => 'date',
				],
				'email' => [
					'field' => 'email',
					'sanitize' => 'email',
				],
				// if you want to set page values dynamically with code, you can use a key name that doesn't match any
				// column name/header from the import file:
				'import_timestamp' => [
					'callback' => function($page, $field_name, $value, $args) {
						$page->import_timestamp = date('Y-m-d H:i:s');
					},
				],
			],
			'limit' => 10,
			'on_duplicate' => 'make_unique',
			'on_missing_page_ref' => 'create',
			'reader_settings' => [
				'delimiter' => ',',
				'enclosure' => '"',
			],
		],
	],
	// set to true if users should be able to provide custom profile configuration settings runtime;
	// please note, though, that this is an advanced option that should only be used when necessary!
	'allow_overriding_profile_configuration' => false,
];

				'title' => [
					'sanitize' => 'text',
				],

				'start_date' => [
					'field' => 'start_date',
					'sanitize' => function($value, $args) {
						if (!empty($value) && is_string($value)) {
							// remove extraneous day abbreviation (e.g. "mon 1.1.2023") from date
							$value = preg_replace('/^[a-z]+ */i', '', $value);
						}
						return wire()->sanitizer->date($value);
					}
				],

				[
					'callback' => function($page, $field, $value, $args) {
						// time provided as a separate column, but we want to combine it with date
						$page->start_date = implode(' ', array_filter([
							date('j.n.Y', $page->getUnformatted('start_date')),
							$value,
						]));
					}
				],

				[
					'callback' => function($page, $field, $value, $args) {
						if (!$page->id) return 'after_save';
						if (empty(trim($value))) return;
						$block = $page->getUnformatted('content_blocks')->getNew();
						$block->setMatrixType('text_content_block');
						$block->text_content = '<p>'
							. implode('</p><p>', array_filter(preg_split("/\r\n|\r|\n/", $value)))
							. '</p>';
						$page->save('content_blocks');
					},
				],

			'is_duplicate' => 'parent={page.parent}, start_date={page.start_date}, title={page.title}',

$config->ImportTools = [
	'profiles' => [
		'members' => [
			'label' => 'Members',
			'notes' => 'title,first_name,last_name,date_of_birth,email',

	'profiles' => [
		'members' => [
			'hooks' => [
				'before_import_page' => function(HookEvent $event) {
					$data = $event->arguments(0);
					$existing_page = wire()->pages->findOne('member_id=' . (int) $data['id']);
					if (!$existing_page->id) return;
					$existing_page->_import_tool_action = 'skipped';
					$event->replace = true;
					$event->return = $existing_page;
				},
			],