summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/docs/technical/code-snippets/hook.datatype.inittypes.md
blob: 67316e65ff954d2d233217be91b4cdfc5c48b0d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
## Register new datatype

This example shows how to register a new dataType/dataValue in Semantic MediaWiki and the convention for the datatype key is to use `___` as leading identifer to distinguish them from those defined by Semantic MediaWiki itself.

### SMW::DataType::initTypes

```php
use Hooks;
use Foo\DataValues\FooValue;

Hooks::register( 'SMW::DataType::initTypes', function ( $dataTypeRegistry ) {

	$dataTypeRegistry->registerDatatype(
		FooValue::TYPE_ID,
		FooValue::class,
		DataItem::TYPE_BLOB
	);

	$dataTypeRegistry->setOption(
		'foovalue.SomeSetting',
		42
	);

	return true;
};
```

### DataValue representation

```php
class FooValue extends DataValue {

	/**
	 * DV identifier
	 */
	const TYPE_ID = '___foo_bar';

	/**
	 * @see DataValue::parseUserValue
	 * @note called by DataValue::setUserValue
	 *
	 * @param string $value
	 */
	protected function parseUserValue( $userValue ) {
		...
	}
}
```

### Usage

```php
$fooValue = DataValueFactory::getInstance()->newTypeIdValue(
	'___foo_bar',
	'Bar'
)

$fooValue->getShortWikiText();
```