forked from adrianbj/processwire-fieldtype-assisted-url
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldtypeAssistedURL.module
101 lines (94 loc) · 2.77 KB
/
FieldtypeAssistedURL.module
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* Class FieldtypeAssistedURL
*
* @author Marco Stoll <m.stoll@core4.de>
* @link http://core4.de CORE4 GmbH & Co. KG
* @copyright Copyright (c) 2015, CORE4 GmbH & Co. KG
* @license MIT http://opensource.org/licenses/MIT
* @version 1.0.0
* @see http://www.processwire.com
* @filesource
*/
/**
* Class FieldtypeAssistedURL
*/
class FieldtypeAssistedURL extends FieldtypeURL
{
/**
* Get information about this module
*
* @return array
*/
public static function getModuleInfo()
{
return [
'title' => 'AssistedURL Field',
'version' => 100,
'summary' => 'Field that stores an URL providing controls for selecting internal pages and files.',
'installs' => 'InputfieldAssistedURL',
];
}
/**
* Per Module interface, this template method is called when all system classes are loaded and ready for API usage
*/
public function init()
{
parent::init();
}
/**
* Return new instance of the Inputfield associated with this Fieldtype
*
* @param Page $page
* @param Field $field
* @return Inputfield
*/
public function getInputfield(Page $page, Field $field)
{
/** @var InputfieldAssistedURL $inputField */
$inputField = $this->modules->get("InputfieldAssistedURL");
$inputField->setPage($page);
return $inputField;
}
/**
* get values converted when fetched from db
*
*/
public function ___wakeupValue(Page $page, Field $field, $value) {
$urlParts = explode("?", $value);
if(is_numeric($urlParts[0])) {
$return = $this->wire('pages')->get($urlParts[0])->url . (isset($urlParts[1]) ? '?' . $urlParts[1] : '');
}
else {
$return = $value;
}
return $return;
}
/**
* return converted from object to array for storing in database
*
*/
public function ___sleepValue(Page $page, Field $field, $value) {
foreach($this->wire('config')->httpHosts as $host) {
if (strpos($value, $host) !== false) {
$value = str_replace($host, '', $value);
$value = preg_replace('#^https?://#', '', $value);
}
}
if(strpos($value, '//') === false) {
$urlParts = explode("?", $value);
$urlPage = "/" . str_replace($this->wire('config')->urls->root, "", $urlParts[0]);
$p = $this->wire('pages')->get(trim($urlPage, '.'));
if($p->id) {
$return = $p->id . (isset($urlParts[1]) ? '?' . $urlParts[1] : '');
}
else {
$return = $value;
}
}
else {
$return = $value;
}
return $return;
}
}