Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
P
Project
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Jobs
Commits
Open sidebar
Gorodkov Denis
Project
Commits
2807af60
Commit
2807af60
authored
May 23, 2022
by
Gorodkov Denis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ajax Form
parent
733de4d5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
111 additions
and
28 deletions
+111
-28
mymodule.install
app/docroot/modules/custom/mymodule/mymodule.install
+24
-21
mymodule.module
app/docroot/modules/custom/mymodule/mymodule.module
+10
-3
AjaxForm.php
app/docroot/modules/custom/mymodule/src/Form/AjaxForm.php
+77
-4
No files found.
app/docroot/modules/custom/mymodule/mymodule.install
View file @
2807af60
...
...
@@ -4,39 +4,42 @@
* Implements hook_schema().
*/
function
mymodule_schema
()
{
$schema
[
'aaaa'
]
=
array
(
// Название таблицы
'description'
=>
'Database example'
,
// Описание таблицы
'fields'
=>
array
(
// Массив с колонками таблицы
'id'
=>
array
(
// Название колонки
'description'
=>
'ID'
,
// Описание колонки
'type'
=>
'serial'
,
// Тип данных
'unsigned'
=>
TRUE
,
// Unsigned, по умолчанию FALSE
'not null'
=>
TRUE
,
// Проверка на 0
),
'uid'
=>
array
(
'description'
=>
'UID user'
,
'type'
=>
'int'
,
$schema
[
'AAA_custom_table'
]
=
array
(
'description'
=>
'A table to store simple data'
,
'fields'
=>
array
(
'id'
=>
array
(
'description'
=>
'Holds the id value'
,
'type'
=>
'serial'
,
'unsigned'
=>
TRUE
,
'not null'
=>
TRUE
,
'default'
=>
0
,
// Значение по умолчанию
),
'
text
'
=>
array
(
'description'
=>
'
Text
'
,
'
name
'
=>
array
(
'description'
=>
'
Holds the name value
'
,
'type'
=>
'varchar'
,
'length'
=>
255
,
'length'
=>
'255'
,
'not null'
=>
TRUE
,
),
'email'
=>
array
(
'description'
=>
'Holds the name value'
,
'type'
=>
'varchar'
,
'length'
=>
'255'
,
'not null'
=>
TRUE
,
),
'url'
=>
array
(
'description'
=>
'Holds the name value'
,
'type'
=>
'varchar'
,
'length'
=>
'255'
,
'not null'
=>
TRUE
,
'default'
=>
''
,
),
),
'primary key'
=>
array
(
'id'
),
);
return
$schema
;
}
/**
* Implements hook_uninstall().
*/
/*
function mymodule_uninstall() {
\Drupal::state()->delete('mymodule.
mymodu
le');
}
*/
function
mymodule_uninstall
()
{
\Drupal
::
state
()
->
delete
(
'mymodule.
AAA_custom_tab
le'
);
}
app/docroot/modules/custom/mymodule/mymodule.module
View file @
2807af60
...
...
@@ -75,12 +75,19 @@ function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_
/**
* Implements hook_node_view().
*/
function
mymodule_node_view
(
array
&
$build
,
\Drupal\Core\Entity\EntityInterface
$entity
,
\Drupal\Core\Entity\Display\EntityViewDisplayInterface
$display
,
$view_mode
)
{
/*
function mymodule_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
$bundle = $entity->bundle();
if ($bundle == 'people') {
$build['#cache']['contexts'][] = 'url.query_args';
$entity->save();
}
}
}*/
/**
* Implements hook_preprocess().
*/
function
mymodule_preprocess
(
&
$variables
,
$hook
)
{
if
(
$hook
==
'node'
)
{
$variables
[
"#cache"
][
"contexts"
][]
=
"url.query_args"
;
}
}
app/docroot/modules/custom/mymodule/src/Form/AjaxForm.php
View file @
2807af60
...
...
@@ -2,6 +2,9 @@
namespace
Drupal\mymodule\Form
;
use
Drupal\Core\Ajax\AjaxResponse
;
use
Drupal\Core\Ajax\CssCommand
;
use
Drupal\Core\Ajax\HtmlCommand
;
use
Drupal\Core\Form\FormBase
;
use
Drupal\Core\Form\FormStateInterface
;
...
...
@@ -14,29 +17,99 @@ class AjaxForm extends FormBase {
$form
[
'name'
]
=
array
(
'#type'
=>
'textfield'
,
'#title'
=>
'Name'
,
'#required'
=>
TRUE
'#required'
=>
TRUE
,
'#prefix'
=>
''
,
'#suffix'
=>
'<div class="name-validation-message"></div>'
,
);
$form
[
'email'
]
=
array
(
'#type'
=>
'email'
,
'#title'
=>
'Email'
,
'#required'
=>
TRUE
'#required'
=>
TRUE
,
'#prefix'
=>
''
,
'#suffix'
=>
'<div class="email-validation-message"></div>'
,
'#ajax'
=>
[
'callback'
=>
[
$this
,
'validateEmailCallbackAjax'
],
'event'
=>
'change'
,
'wrapper'
=>
'edit-email'
,
],
);
$form
[
'url'
]
=
array
(
'#type'
=>
'textfield'
,
'#title'
=>
'URL'
,
'#required'
=>
TRUE
'#required'
=>
TRUE
,
'#prefix'
=>
''
,
'#suffix'
=>
'<div class="url-validation-message"></div>'
,
'#ajax'
=>
[
'callback'
=>
[
$this
,
'validateUrlCallbackAjax'
],
'event'
=>
'change'
,
'wrapper'
=>
'edit-email'
,
],
);
$form
[
'submit'
]
=
array
(
'#type'
=>
'submit'
,
'#value'
=>
'
Import'
'#value'
=>
'
Click'
,
);
return
$form
;
}
public
function
validateForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
$url
=
$form_state
->
getValue
(
'url'
);
$regexp
=
'/^(https|http):\/\//'
;
$checkHttp
=
(
bool
)
preg_match
(
$regexp
,
$url
);
if
(
$checkHttp
)
{
$url
[
4
]
==
's'
?
$httpOrHttps
=
8
:
$httpOrHttps
=
7
;
$checkHttp
?
$url
=
substr
(
$url
,
$httpOrHttps
)
:
''
;
}
$regexp
=
'/^www./'
;
$checkWww
=
((
bool
)
preg_match
(
$regexp
,
$url
));
if
(
$checkWww
)
{
$url
=
substr
(
$url
,
4
);
}
$regexp
=
'/^([a-z0-9]*.[rр][uф])/'
;
$checkUrl
=
(
bool
)
preg_match
(
$regexp
,
$url
);
$url
=
explode
(
'.'
,
$url
);
$url
=
$url
[
0
]
.
'.'
.
$url
[
1
];
}
public
function
submitForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
$query
=
\Drupal
::
database
()
->
insert
(
'AAA_custom_table'
);
$query
->
fields
([
'name'
=>
$form_state
->
getValue
(
'name'
),
'email'
=>
$form_state
->
getValue
(
'email'
),
'url'
=>
$form_state
->
getValue
(
'url'
),
]);
$query
->
execute
();
}
public
function
validateEmailCallbackAjax
(
array
&
$form
,
FormStateInterface
$form_state
)
{
$response
=
new
AjaxResponse
();
$email
=
$form_state
->
getValue
(
'email'
);
$email_valid
=
filter_var
(
$email
,
FILTER_VALIDATE_EMAIL
);
if
(
!
$email_valid
and
!
empty
(
$email
))
{
$response
->
addCommand
(
new
HtmlCommand
(
'.email-validation-message'
,
'Введен не корректный email'
));
$response
->
addCommand
(
new
CssCommand
(
'.email-validation-message'
,
[
'color'
=>
'red'
]));
$response
->
addCommand
(
new
CssCommand
(
'#edit-email'
,
[
'border'
=>
'1px solid red'
]));
}
else
{
$response
->
addCommand
(
new
HtmlCommand
(
'.email-validation-message'
,
''
));
$response
->
addCommand
(
new
CssCommand
(
'#edit-email'
,
[
'border'
=>
''
]));
}
return
$response
;
}
public
function
validateUrlCallbackAjax
(
array
&
$form
,
FormStateInterface
$formState
)
{
$response
=
new
AjaxResponse
();
$response
->
addCommand
(
new
HtmlCommand
(
'.url-validation-message'
,
'ZXC'
));
$response
->
addCommand
(
new
CssCommand
(
'#edit-url'
,
[
'border'
=>
'1px solid red'
]));
return
$response
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment