DreamR1der.ru - создание модулей
http://dreamr1der.ru/tags/%D1%81%D0%BE%D0%B7%D0%B4%D0%B0%D0%BD%D0%B8%D0%B5-%D0%BC%D0%BE%D0%B4%D1%83%D0%BB%D0%B5%D0%B9
 
 ru

 Drupal API offline
 http://dreamr1der.ru/drupal_api_offline
 <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>Время от времени мне бывает нужен доступ к документации api друпала при отсутствии инета. Пошарившись в гугле, я набрел на проект <a href="https://drupal.org/project/drupalapi">DrupalAPI</a>. Разворачивается как обычный друпал-сайт у себя на локалхосте. Доступна справка по функциям шестого и седьмого друпала, восьмой наверно позже добавят. Есть поиск с автодополнением. Единственный минус - нет комментариев, в них много интересного бывает, так что онлайн версия все равно лучше.<br /><a href="/sites/default/files/posts_image/drupal_api_offline/drupalapi_front.png"><img src="/sites/default/files/posts_image/drupal_api_offline/drupalapi_front.png" alt="Drupal API Front" /></a><br />
Не забудьте перед установкой сайта выставить PHP memory_limit минимум 256M, о чем сказано на странице проекта. Я проигнорировал данный совет, оставив 128М, в результате чего незадолго до завершения установки скрипт вылетел и не заводился, пока я не увеличил лимит до требуемого.</p>
<h2>PHP Docs-to-go</h2>
<p>Кстати, для оффлайновой справки по PHP я использую расширение для Chrome <a href="https://chrome.google.com/webstore/detail/php-docs-to-go/mlilmganaobieaclflbciblffhaagnip">PHP Docs-to-go</a>. Присутствует поиск по всем функциям, а также комментарии пользователей (с php.net, как я понял).<br /><img src="/sites/default/files/posts_image/drupal_api_offline/chrome_php_offline.png" alt="PHP Docs-to-go" /></p>
</div></div></div><div class="field field-name-field-tags field-type-taxonomy-term-reference field-label-above"><div class="field-label">Теги:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tags/drupal7" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">drupal7</a></div><div class="field-item odd"><a href="/tags/drupal6" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">drupal6</a></div><div class="field-item even"><a href="/tags/%D1%81%D0%BE%D0%B7%D0%B4%D0%B0%D0%BD%D0%B8%D0%B5-%D0%BC%D0%BE%D0%B4%D1%83%D0%BB%D0%B5%D0%B9" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">создание модулей</a></div></div></div>
 Fri, 16 May 2014 19:22:12 +0000
 DreamR1der
 21 at http://dreamr1der.ru
 http://dreamr1der.ru/drupal_api_offline#comments


 Пример создания собственного типа поля для Drupal7
 http://dreamr1der.ru/drupal_custom_field_type
 <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>В этой статье я покажу, как с помощью своего модуля можно добавить новый тип поля. Модуль будет называется Contact Field и позволит добавлять к ноде адрес, телефон, email и адрес сайта. Вывод будет двух типов: обычный текст и ссылки:  значение с адресом будет вести на поиск в яндекс-картах, поле с сайтом на сайт (логично :), а для ссылок с емейлом и телефоном будут добавлены атрибуты 'mailto' и 'tel' соответственно.  Также я покажу, как сделать проверку корректности вводимых значений. </p>
<p>Как это выглядит:<br /><a href="/sites/default/files/posts_image/contact_field_1.png"><img src="/sites/default/files/styles/medium/public/posts_image/contact_field_1.png?itok=ROA79t5W" width="220" height="124" alt="" class="image-medium" /></a><br /><a href="/sites/default/files/posts_image/contact_field_2.png"><img src="/sites/default/files/styles/medium/public/posts_image/contact_field_4.png?itok=3h4oNH6o" width="220" height="181" alt="" class="image-medium" /></a><br /><a href="/sites/default/files/posts_image/contact_field_3.png"><img src="/sites/default/files/styles/medium/public/posts_image/contact_field_3.png?itok=CnCIOxwE" width="220" height="162" alt="" class="image-medium" /></a><br /><a href="/sites/default/files/posts_image/contact_field_4.png"><img src="/sites/default/files/styles/medium/public/posts_image/contact_field_2.png?itok=N3N2lJ6z" width="220" height="132" alt="" class="image-medium" /></a></p>
<p>Идем в 'sites/modules/', создаем директорию 'contact_field', а в ней два файла: 'contact_field.info' и 'contact_field.module'.</p>
<p>Файл 'contact_field.info':</p>
<div class="codeblock"><code>name = Contact Field<br />description = "Creates a custom field for inputting and displaying contacts (website, address, phone and email"<br />package = Examples<br />core = 7.x<br />files[] = contact_field.module<br />php = 5.2</code></div>
<p>Далее сам модуль (файл 'contact_field.module'). Сначала зададим общие настройки для поля: название, виджет по умолчанию (форма ввода на странице редактирования ноды) и способ отображения.</p>
<div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php
<p></p></span><span style="color: #FF8000">/**<br />* @file<br />* An example field using the Field API.<br />*<br />*/
<p>/**<br /> * Implements hook_field_info().<br /> *<br /> * Provides the description of the field<br /> * Базовые настройки нашего поля, виджет по умолчанию (форма ввода контакта) и форматирование (отображение посетителю)<br /> */<br /></p></span><span style="color: #007700">function </span><span style="color: #0000BB">contact_field_field_info</span><span style="color: #007700">() {<br />    return array(<br />        </span><span style="color: #DD0000">'Contact Field' </span><span style="color: #007700">=&gt; array(<br />            </span><span style="color: #DD0000">'label' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Contacts'</span><span style="color: #007700">),<br />            </span><span style="color: #DD0000">'description' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Contact Description'</span><span style="color: #007700">),<br />            </span><span style="color: #DD0000">'default_widget' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'contact_input'</span><span style="color: #007700">,<br />            </span><span style="color: #DD0000">'default_formatter' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'contact_links'</span><span style="color: #007700">,<br />        ),<br />    );<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
<p>Далее зададим способ хранения в базе данных. В данном случае будет колонка 'contact' (тип varchar, длина 255), где будет храниться контакт, вводимый пользователем, и колонка 'type' (varchar, длина 100), где будет хранится тип контакта (емайл, адрес, сайт или телефон).</p>
<div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/**<br /> * Implements hook_field_schema().<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">contact_field_field_schema</span><span style="color: #007700">(</span><span style="color: #0000BB">$field</span><span style="color: #007700">){<br />    </span><span style="color: #0000BB">$columns </span><span style="color: #007700">= array(<br />        </span><span style="color: #DD0000">'contact' </span><span style="color: #007700">=&gt; array(<br />            </span><span style="color: #DD0000">'type' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'varchar'</span><span style="color: #007700">,<br />            </span><span style="color: #DD0000">'length' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">255</span><span style="color: #007700">,<br />            </span><span style="color: #DD0000">'not null' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">,<br />        ),<br />        </span><span style="color: #DD0000">'type' </span><span style="color: #007700">=&gt; array(<br />            </span><span style="color: #DD0000">'type' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'varchar'</span><span style="color: #007700">,<br />            </span><span style="color: #DD0000">'length' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">100</span><span style="color: #007700">,<br />            </span><span style="color: #DD0000">'not null' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">,<br />        )<br />    );<br />    </span><span style="color: #0000BB">$indexes </span><span style="color: #007700">= array(<br />        </span><span style="color: #DD0000">'contact' </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">'contact'</span><span style="color: #007700">),<br />    );<br />    return array(<br />        </span><span style="color: #DD0000">'columns' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$columns</span><span style="color: #007700">,<br />        </span><span style="color: #DD0000">'indexes' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$indexes</span><span style="color: #007700">,<br />    );<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
<p>Теперь надо проверить, что юзер там ввел, и если что на так, показать сообщение об ошибке. В данном случае валидация ввода будет разная, в зависимости от типа контакта. Я сделал валидацию только для типа 'email', но общий принцип должен быть понятен.</p>
<div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/**<br /> * Implements hook_field_validate().<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">contact_field_field_validate</span><span style="color: #007700">(</span><span style="color: #0000BB">$entity_type</span><span style="color: #007700">, </span><span style="color: #0000BB">$entity</span><span style="color: #007700">, </span><span style="color: #0000BB">$field</span><span style="color: #007700">, </span><span style="color: #0000BB">$instance</span><span style="color: #007700">, </span><span style="color: #0000BB">$langcode</span><span style="color: #007700">, </span><span style="color: #0000BB">$items</span><span style="color: #007700">, &amp;</span><span style="color: #0000BB">$errors</span><span style="color: #007700">){<br />    foreach(</span><span style="color: #0000BB">$items </span><span style="color: #007700">as </span><span style="color: #0000BB">$delta </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$item</span><span style="color: #007700">){<br />        </span><span style="color: #FF8000">// Проверка значений в зависимости от поля type<br />        </span><span style="color: #007700">switch(</span><span style="color: #0000BB">$item</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">]){<br />            case </span><span style="color: #DD0000">'email'</span><span style="color: #007700">:<br />                if (!</span><span style="color: #0000BB">valid_email_address</span><span style="color: #007700">(</span><span style="color: #0000BB">$item</span><span style="color: #007700">[</span><span style="color: #DD0000">'contact'</span><span style="color: #007700">])){<br />                    </span><span style="color: #0000BB">$errors</span><span style="color: #007700">[</span><span style="color: #0000BB">$field</span><span style="color: #007700">[</span><span style="color: #DD0000">'field_name'</span><span style="color: #007700">]][</span><span style="color: #0000BB">$langcode</span><span style="color: #007700">][</span><span style="color: #0000BB">$delta</span><span style="color: #007700">][] = array(<br />                        </span><span style="color: #DD0000">'error' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'contact_invalid'</span><span style="color: #007700">,<br />                        </span><span style="color: #DD0000">'message' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Invalid E-mail address'</span><span style="color: #007700">),<br />                    );<br />                }<br />                break;<br />            case </span><span style="color: #DD0000">'phone'</span><span style="color: #007700">:<br />                </span><span style="color: #FF8000">// TODO мне было лень писать проверку телефона<br />                </span><span style="color: #007700">break;<br />            case </span><span style="color: #DD0000">'site'</span><span style="color: #007700">:<br />                </span><span style="color: #FF8000">// TODO и проверку адреса сайта тоже<br />                </span><span style="color: #007700">break;<br />        }<br />    }<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
<p>Можно указать, при каких условиях считать поле пустым и не сохранять его, либо удалить уже сохраненное поле. В данном случае, если контакт пуст, то оно не нужно, хотя значение типа контакта у нас всегда будет иметь какое-либо значение.  </p>
<div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*<br /> * Implements hook_field_is_empty().<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">contact_field_field_is_empty</span><span style="color: #007700">(</span><span style="color: #0000BB">$item</span><span style="color: #007700">, </span><span style="color: #0000BB">$field</span><span style="color: #007700">) {<br />    return empty(</span><span style="color: #0000BB">$item</span><span style="color: #007700">[</span><span style="color: #DD0000">'contact'</span><span style="color: #007700">]);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
<p>Зададим варианты отображения поля (ссылками или простым текстом) и подключим свой файл css/contact-field.css, чтобы сделать оформление контактов поинтереснее. </p>
<div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/**<br /> * Implements hook_field_formatter_info().<br /> *<br /> * Варианты отображение контактов<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">contact_field_field_formatter_info</span><span style="color: #007700">(){<br />    return array(<br />        </span><span style="color: #FF8000">// отображение в виде простого текста<br />        </span><span style="color: #DD0000">'contact_text' </span><span style="color: #007700">=&gt; array(<br />            </span><span style="color: #DD0000">'label' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Contact simple text'</span><span style="color: #007700">),<br />            </span><span style="color: #DD0000">'field types' </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">'Contact Field'</span><span style="color: #007700">),<br />        ),<br />        </span><span style="color: #FF8000">// отображение в виде ссылок<br />        </span><span style="color: #DD0000">'contact_links' </span><span style="color: #007700">=&gt; array(<br />            </span><span style="color: #DD0000">'label' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Contact links'</span><span style="color: #007700">),<br />            </span><span style="color: #DD0000">'field types' </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">'Contact Field'</span><span style="color: #007700">),<br />        ),<br />    );<br />}
<p></p></span><span style="color: #FF8000">/**<br /> * Implements hook_field_formatter_view().<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">contact_field_field_formatter_view</span><span style="color: #007700">(</span><span style="color: #0000BB">$entity_type</span><span style="color: #007700">, </span><span style="color: #0000BB">$entity</span><span style="color: #007700">, </span><span style="color: #0000BB">$field</span><span style="color: #007700">, </span><span style="color: #0000BB">$instance</span><span style="color: #007700">, </span><span style="color: #0000BB">$langcode</span><span style="color: #007700">, </span><span style="color: #0000BB">$items</span><span style="color: #007700">, </span><span style="color: #0000BB">$display</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$element </span><span style="color: #007700">= array();
<p>    switch (</p></span><span style="color: #0000BB">$display</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">]){<br />        </span><span style="color: #FF8000">// отображение контактов в виде текста<br />        </span><span style="color: #007700">case </span><span style="color: #DD0000">'contact_text'</span><span style="color: #007700">:<br />            </span><span style="color: #0000BB">drupal_add_css</span><span style="color: #007700">(</span><span style="color: #0000BB">drupal_get_path</span><span style="color: #007700">(</span><span style="color: #DD0000">'module'</span><span style="color: #007700">, </span><span style="color: #DD0000">'contact_field'</span><span style="color: #007700">) . </span><span style="color: #DD0000">'/css/contact-field.css'</span><span style="color: #007700">);<br />            foreach (</span><span style="color: #0000BB">$items </span><span style="color: #007700">as </span><span style="color: #0000BB">$delta </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$item</span><span style="color: #007700">) {<br />                </span><span style="color: #0000BB">$contact </span><span style="color: #007700">= </span><span style="color: #0000BB">strip_tags</span><span style="color: #007700">(</span><span style="color: #0000BB">$item</span><span style="color: #007700">[</span><span style="color: #DD0000">'contact'</span><span style="color: #007700">]);<br />                switch(</span><span style="color: #0000BB">$item</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">]){<br />                    case </span><span style="color: #DD0000">'address'</span><span style="color: #007700">:<br />                        </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #0000BB">$delta</span><span style="color: #007700">][</span><span style="color: #DD0000">'#markup'</span><span style="color: #007700">] =<br />                            </span><span style="color: #DD0000">'&lt;p class="contact-field-address"&gt;' </span><span style="color: #007700">. </span><span style="color: #0000BB">$contact </span><span style="color: #007700">. </span><span style="color: #DD0000">'&lt;/p&gt;'</span><span style="color: #007700">;<br />                        break;<br />                    case </span><span style="color: #DD0000">'site'</span><span style="color: #007700">:<br />                        </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #0000BB">$delta</span><span style="color: #007700">][</span><span style="color: #DD0000">'#markup'</span><span style="color: #007700">] =<br />                            </span><span style="color: #DD0000">'&lt;p class="contact-field-site"&gt;' </span><span style="color: #007700">. </span><span style="color: #0000BB">$contact </span><span style="color: #007700">. </span><span style="color: #DD0000">'&lt;/p&gt;'</span><span style="color: #007700">;<br />                        break;<br />                    case </span><span style="color: #DD0000">'email'</span><span style="color: #007700">:<br />                        </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #0000BB">$delta</span><span style="color: #007700">][</span><span style="color: #DD0000">'#markup'</span><span style="color: #007700">] =<br />                            </span><span style="color: #DD0000">'&lt;p class="contact-field-email"&gt;' </span><span style="color: #007700">. </span><span style="color: #0000BB">$contact </span><span style="color: #007700">. </span><span style="color: #DD0000">'&lt;/p&gt;'</span><span style="color: #007700">;<br />                        break;<br />                    case </span><span style="color: #DD0000">'phone'</span><span style="color: #007700">:<br />                        </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #0000BB">$delta</span><span style="color: #007700">][</span><span style="color: #DD0000">'#markup'</span><span style="color: #007700">] =<br />                            </span><span style="color: #DD0000">'&lt;p class="contact-field-phone"&gt;' </span><span style="color: #007700">. </span><span style="color: #0000BB">$contact </span><span style="color: #007700">. </span><span style="color: #DD0000">'&lt;/p&gt;'</span><span style="color: #007700">;<br />                        break;<br />                }<br />            }<br />            break;<br />        </span><span style="color: #FF8000">// отображение контактов ссылками<br />        </span><span style="color: #007700">case </span><span style="color: #DD0000">'contact_links'</span><span style="color: #007700">:<br />            </span><span style="color: #0000BB">drupal_add_css</span><span style="color: #007700">(</span><span style="color: #0000BB">drupal_get_path</span><span style="color: #007700">(</span><span style="color: #DD0000">'module'</span><span style="color: #007700">, </span><span style="color: #DD0000">'contact_field'</span><span style="color: #007700">) . </span><span style="color: #DD0000">'/css/contact-field.css'</span><span style="color: #007700">);<br />            foreach (</span><span style="color: #0000BB">$items </span><span style="color: #007700">as </span><span style="color: #0000BB">$delta </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$item</span><span style="color: #007700">) {<br />                </span><span style="color: #0000BB">$contact </span><span style="color: #007700">= </span><span style="color: #0000BB">strip_tags</span><span style="color: #007700">(</span><span style="color: #0000BB">$item</span><span style="color: #007700">[</span><span style="color: #DD0000">'contact'</span><span style="color: #007700">]);<br />                switch(</span><span style="color: #0000BB">$item</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">]){<br />                    case </span><span style="color: #DD0000">'address'</span><span style="color: #007700">:<br />                        </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #0000BB">$delta</span><span style="color: #007700">][</span><span style="color: #DD0000">'#markup'</span><span style="color: #007700">] =<br />                            </span><span style="color: #DD0000">'&lt;p class="contact-field-address"&gt;<br />                            &lt;a href="http://maps.yandex.ru/?text=' </span><span style="color: #007700">. </span><span style="color: #0000BB">$contact </span><span style="color: #007700">. </span><span style="color: #DD0000">'"&gt;' </span><span style="color: #007700">. </span><span style="color: #0000BB">$contact </span><span style="color: #007700">. </span><span style="color: #DD0000">'&lt;/a&gt;&lt;/p&gt;'</span><span style="color: #007700">;<br />                        break;<br />                    case </span><span style="color: #DD0000">'site'</span><span style="color: #007700">:<br />                        </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #0000BB">$delta</span><span style="color: #007700">][</span><span style="color: #DD0000">'#markup'</span><span style="color: #007700">] =<br />                            </span><span style="color: #DD0000">'&lt;p class="contact-field-site"&gt;&lt;a href="http://' </span><span style="color: #007700">. </span><span style="color: #0000BB">$contact </span><span style="color: #007700">. </span><span style="color: #DD0000">'"&gt;' </span><span style="color: #007700">. </span><span style="color: #0000BB">$contact </span><span style="color: #007700">. </span><span style="color: #DD0000">'&lt;/a&gt;&lt;/p&gt;'</span><span style="color: #007700">;<br />                        break;<br />                    case </span><span style="color: #DD0000">'email'</span><span style="color: #007700">:<br />                        </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #0000BB">$delta</span><span style="color: #007700">][</span><span style="color: #DD0000">'#markup'</span><span style="color: #007700">] =<br />                            </span><span style="color: #DD0000">'&lt;p class="contact-field-email"&gt;&lt;a href="mailto:' </span><span style="color: #007700">. </span><span style="color: #0000BB">$contact </span><span style="color: #007700">. </span><span style="color: #DD0000">'"&gt;' </span><span style="color: #007700">. </span><span style="color: #0000BB">$contact </span><span style="color: #007700">. </span><span style="color: #DD0000">'&lt;/a&gt;&lt;/p&gt;'</span><span style="color: #007700">;<br />                        break;<br />                    case </span><span style="color: #DD0000">'phone'</span><span style="color: #007700">:<br />                        </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #0000BB">$delta</span><span style="color: #007700">][</span><span style="color: #DD0000">'#markup'</span><span style="color: #007700">] =<br />                            </span><span style="color: #DD0000">'&lt;p class="contact-field-phone"&gt;&lt;a href="tel:' </span><span style="color: #007700">. </span><span style="color: #0000BB">$contact </span><span style="color: #007700">. </span><span style="color: #DD0000">'"&gt;' </span><span style="color: #007700">. </span><span style="color: #0000BB">$contact </span><span style="color: #007700">. </span><span style="color: #DD0000">'&lt;/a&gt;&lt;/p&gt;'</span><span style="color: #007700">;<br />                        break;<br />                }<br />            }<br />            break;<br />    }<br />    return </span><span style="color: #0000BB">$element</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
<p>Для ввода значений в форме редактирования ноды также можно сделать несколько вариантов, но у меня будет только один: текстовое поле для ввода контакта и селект для выбора типа контакта: емайл, адрес, сайт и телефон.<br /><?code?></p>
<div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/**<br /> * Implements hook_field_widget_info().<br /> * Форма ввода контактов.<br /> * В данном случае вариант только один - простое текстовое поле и селект для типа контакта.<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">contact_field_field_widget_info</span><span style="color: #007700">(){<br />    return array(<br />        </span><span style="color: #DD0000">'contact_input' </span><span style="color: #007700">=&gt; array(<br />            </span><span style="color: #DD0000">'label' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Contact Input'</span><span style="color: #007700">),<br />            </span><span style="color: #DD0000">'field types' </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">'Contact Field'</span><span style="color: #007700">),<br />        ),<br />    );<br />}
<p></p></span><span style="color: #FF8000">/**<br /> * Implements hook_field_widget_form().<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">contact_field_field_widget_form</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">, &amp;</span><span style="color: #0000BB">$form_state</span><span style="color: #007700">, </span><span style="color: #0000BB">$field</span><span style="color: #007700">, </span><span style="color: #0000BB">$instance</span><span style="color: #007700">, </span><span style="color: #0000BB">$langcode</span><span style="color: #007700">, </span><span style="color: #0000BB">$items</span><span style="color: #007700">, </span><span style="color: #0000BB">$delta</span><span style="color: #007700">, </span><span style="color: #0000BB">$element</span><span style="color: #007700">) {<br />    </span><span style="color: #FF8000">// значение по умолчанию для значения контакта<br />    </span><span style="color: #0000BB">$contact_value </span><span style="color: #007700">= isset(</span><span style="color: #0000BB">$items</span><span style="color: #007700">[</span><span style="color: #0000BB">$delta</span><span style="color: #007700">][</span><span style="color: #DD0000">'contact'</span><span style="color: #007700">]) ? </span><span style="color: #0000BB">$items</span><span style="color: #007700">[</span><span style="color: #0000BB">$delta</span><span style="color: #007700">][</span><span style="color: #DD0000">'contact'</span><span style="color: #007700">] : </span><span style="color: #DD0000">''</span><span style="color: #007700">;
<p>    </p></span><span style="color: #FF8000">// значение по умолчанию для типа контакта<br />    </span><span style="color: #0000BB">$type_value </span><span style="color: #007700">= isset(</span><span style="color: #0000BB">$items</span><span style="color: #007700">[</span><span style="color: #0000BB">$delta</span><span style="color: #007700">][</span><span style="color: #DD0000">'type'</span><span style="color: #007700">]) ? </span><span style="color: #0000BB">$items</span><span style="color: #007700">[</span><span style="color: #0000BB">$delta</span><span style="color: #007700">][</span><span style="color: #DD0000">'type'</span><span style="color: #007700">] : </span><span style="color: #DD0000">'site'</span><span style="color: #007700">;
<p>    </p></span><span style="color: #0000BB">$element </span><span style="color: #007700">+= array(<br />        </span><span style="color: #DD0000">'#delta' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$delta</span><span style="color: #007700">,<br />    );<br />    </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'contact'</span><span style="color: #007700">] = array();<br />    </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">] = array();
<p>    switch (</p></span><span style="color: #0000BB">$instance</span><span style="color: #007700">[</span><span style="color: #DD0000">'widget'</span><span style="color: #007700">][</span><span style="color: #DD0000">'type'</span><span style="color: #007700">]){<br />        case </span><span style="color: #DD0000">'contact_input'</span><span style="color: #007700">:<br />            </span><span style="color: #FF8000">// текстовое поля для контакта<br />            </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'contact'</span><span style="color: #007700">] += array(<br />                </span><span style="color: #DD0000">'#title' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Enter contact'</span><span style="color: #007700">),<br />                </span><span style="color: #DD0000">'#type' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'textfield'</span><span style="color: #007700">,<br />                </span><span style="color: #DD0000">'#default_value' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$contact_value</span><span style="color: #007700">,<br />                </span><span style="color: #DD0000">'#size' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">100</span><span style="color: #007700">,<br />                </span><span style="color: #DD0000">'#maxlength' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">255</span><span style="color: #007700">,<br />            );<br />            </span><span style="color: #FF8000">// селект для выбора типа<br />            </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">] += array(<br />                </span><span style="color: #DD0000">'#title' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Type'</span><span style="color: #007700">),<br />                </span><span style="color: #DD0000">'#type' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'select'</span><span style="color: #007700">,<br />                </span><span style="color: #DD0000">'#default_value' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$type_value</span><span style="color: #007700">,<br />                </span><span style="color: #DD0000">'#options' </span><span style="color: #007700">=&gt; array(<br />                    </span><span style="color: #DD0000">'site' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Site'</span><span style="color: #007700">),<br />                    </span><span style="color: #DD0000">'address' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Address'</span><span style="color: #007700">),<br />                    </span><span style="color: #DD0000">'phone' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Phone'</span><span style="color: #007700">),<br />                    </span><span style="color: #DD0000">'email' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'E-mail'</span><span style="color: #007700">),<br />                ),<br />            );<br />            break;<br />    }<br />    return </span><span style="color: #0000BB">$element</span><span style="color: #007700">;<br />}
<p></p></span><span style="color: #FF8000">/**<br /> * Implements hook_field_error().<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">contact_field_field_widget_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">, </span><span style="color: #0000BB">$error</span><span style="color: #007700">, </span><span style="color: #0000BB">$form</span><span style="color: #007700">, &amp;</span><span style="color: #0000BB">$form_state</span><span style="color: #007700">) {<br />    switch (</span><span style="color: #0000BB">$error</span><span style="color: #007700">[</span><span style="color: #DD0000">'error'</span><span style="color: #007700">]) {<br />        case </span><span style="color: #DD0000">'contact_invalid'</span><span style="color: #007700">:<br />            </span><span style="color: #0000BB">form_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">, </span><span style="color: #0000BB">$error</span><span style="color: #007700">[</span><span style="color: #DD0000">'message'</span><span style="color: #007700">]);<br />            break;<br />    }<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
<p></p>
<p>файл contact-field.css</p>
<div class="codeblock"><code>.contact-field-address,<br />.contact-field-email,<br />.contact-field-phone,<br />.contact-field-site{<br />    font-size: 25px;<br />    padding-left: 34px;<br />    line-height: 32px;<br />}
<p>.contact-field-address{<br />    background: url(/img/address-icon.png) no-repeat;<br />}<br />.contact-field-email{<br />    background: url(/img/email-icon.png) no-repeat;<br />}<br />.contact-field-phone{<br />    background: url(/img/phone-icon.png) no-repeat;<br />}<br />.contact-field-site{<br />    background: url(/img/website-icon.png) no-repeat;<br />}</p></code></div>
</div></div></div><div class="field field-name-field-tags field-type-taxonomy-term-reference field-label-above"><div class="field-label">Теги:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/tags/drupal7" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">drupal7</a></div><div class="field-item odd"><a href="/tags/%D1%81%D0%BE%D0%B7%D0%B4%D0%B0%D0%BD%D0%B8%D0%B5-%D0%BC%D0%BE%D0%B4%D1%83%D0%BB%D0%B5%D0%B9" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">создание модулей</a></div></div></div>
 Thu, 09 Jan 2014 02:01:09 +0000
 DreamR1der
 9 at http://dreamr1der.ru
 http://dreamr1der.ru/drupal_custom_field_type#comments