To get things started, in this lesson we'll create a new module, and use hook_views_api() to let Views know we want to use its API.
Telling Views About Your Module
Video loading...
-
0:00
[Telling Views About Your Module] [Coding for Views for Drupal 7]
-
0:02
[Chapter 4] [with Addison Berry]
-
0:04
In this video, we're going to get started with working with the Views API.
-
0:08
The very first thing you have to do, no matter what you're going to do
-
0:11
with the Views API, is actually declare that.
-
0:15
We're going to create a very simple, basic module
-
0:18
and do our first Views hook, which will be our hook_views_api
-
0:24
to declare which API we're using.
-
0:28
Before we begin doing any actual code the first thing we're going to want to do
-
0:31
is take a look at the documentation that we have available
-
0:34
so we have some guidance as to what we're doing.
-
0:37
I'm going to go to Advanced help, Views, where the Views documentation
-
0:40
lives on my site and go to the Views API section.
-
0:45
There's a general overview up here, and then if we scroll down
-
0:48
you'll see that the very first most important hook that we need
-
0:52
is the hook_views_api, which we need in order to
-
0:56
declare to Views that, hey, we're using Views, and which version are we using?
-
1:02
Now, we'll also see that there's also documentation for this hook
-
1:06
on API.Drupal.org, so I can go ahead and
-
1:09
type that in to my search here,
-
1:12
and you'll see it's the first one up here, so I'll go ahead and select that.
-
1:16
Load that up, and you'll see we have the documentation coming
-
1:20
directly from the code rather than a book page,
-
1:25
and a code sample, which is also extremely handy
-
1:28
when working with hooks because we can use that, the copy/paste.
-
1:31
Okay, I'm going to go back to my website documentation here,
-
1:34
and we'll look at this hook.
-
1:37
You'll see we have three elements, and the first and the only one
-
1:41
that's required is API version.
-
1:43
We need to tell Views which version of the API we're using
-
1:47
so that we can make sure we're using the right functions
-
1:49
and that things are going to be there when we expect them to be there.
-
1:53
In Drupal 6 we had version 2 and 3.
-
1:55
In Drupal 7 we have version 3.
-
1:57
That's the one we'll be using in this video,
-
2:00
and Views can be pretty smart about this, and if there's newer versions
-
2:04
but your stuff still works, that's great, but we have to definitely declare
-
2:08
which version of the API.
-
2:10
The next thing on the list here is path, and we only need that
-
2:14
if we're going to be including additional files for our views
-
2:19
not directly in the module root folder.
-
2:22
If we're going to be creating a hierarchical structure in our folders
-
2:25
we need to define that here, and then we have the template path
-
2:29
which we'll look at when we get to theming.
-
2:31
We're not going to deal with that now.
-
2:34
I'm going to go into my sites, all, modules.
-
2:38
That's where all my modules live.
-
2:40
I created a custom folder, and I have a Drupalize.Me module I've already created,
-
2:44
and that consists of an info file, standard info file
-
2:48
but with a dependency on Views because we're using the Views API, correct?
-
2:53
And then I have an empty module file,
-
2:55
so this is the bare bones of creating a new module,
-
2:59
and this is where we're going to be storing the Views code
-
3:02
that we're going to be doing in this video and the next.
-
3:05
I'm going to start off by doing a quick DocBlock here for my hook.
-
3:11
This is the standard coding convention for writing a DocBlock for a hook,
-
3:16
and now I'm going to go ahead and put my hook.
-
3:18
Now, I could type it out. It's relatively simple.
-
3:21
But I really like working with the API documentation with hooks
-
3:25
because there are always examples I can copy/paste
-
3:28
and then just edit the way that I need them,
-
3:31
so I'm going to come back here and paste that and put it in here,
-
3:35
and now I need to fix my indentation really quickly here.
-
3:39
Okay, so the first thing we do
-
3:44
when we're using a hook is we change the word hook to our module name,
-
3:47
so this is now Drupalize.Me Views API.
-
3:51
We have it named properly, and now let's review the items
-
3:54
we have in here.
-
3:56
As I said, the API version we're declaring is version 3,
-
4:00
so that's relatively straightforward
-
4:04
and an easy thing to do here.
-
4:06
The path, like I said, I'm going to actually put--
-
4:10
the additional files I'm going to be creating for this,
-
4:14
I'm going to put those in a hierarchical folder structure
-
4:17
just to organize things the way I want.
-
4:19
You don't have to do this if you don't want to.
-
4:21
You can put everything directly in your module folder,
-
4:24
but I'm going to change this out.
-
4:26
I want it to drupal_get_path to my module--
-
4:28
example module does not exist--so that the path sees my module,
-
4:33
and then I'm going to create includes and views folders
-
4:36
once we get to that step of actually creating my default views.
-
4:40
And the template path, we're not going to deal with that.
-
4:42
Again, that has more to do with theming if my Views work
-
4:45
is providing template for the theme, and I'm not going to be doing that right now,
-
4:50
so we can leave that off for now.
-
4:52
The last thing that we need to do in order to make sure all of this stuff
-
4:55
is working, all I've done is declare an API version,
-
4:58
and I've said I have a path for other files you might be interested in,
-
5:02
but the last thing we need to do is actually enable the module.
-
5:05
This isn't going to do me any good if we don't enable at some point,
-
5:08
so I'm going to go ahead and go back to the website,
-
5:11
and we'll go over here to Modules
-
5:15
and then scroll down, and we should see
-
5:21
my handy Drupalize.Me module,
-
5:24
so I'll go ahead and enable that.
-
5:26
And you'll see once it's enabled it's enabled the module.
-
5:29
Nothing has happened because all we've done is declare our API version.
-
5:33
We haven't actually done anything yet, and so that's what we're going to be
-
5:38
looking at next is let's do something with the Views API.
-
0:00
[Falando Para Views Sobre o Seu Módulo] [Codificando para Views para Drupal 7]
-
0:02
[Capítulo 4] [com Addison Berry]
-
0:04
Nesse vídeo, iremos começar a trabalhar com a Views API.
-
0:08
A primeira coisa que você tem que fazer, independente do que você for fazer
-
0:11
com a Views API, é declarar isso.
-
0:15
Iremos criar um módulo bastante simples e básico
-
0:18
e fazer nosso primeiro hook de Views, que será nosso hook_views_api
-
0:24
para declarar qual API estamos usando.
-
0:28
Antes de começarmos a escrever código a primeira coisa que iremos fazer
-
0:31
é dar uma olhada na documentação que temos disponível
-
0:34
para que possamos ter um guia sobre o que estamos fazendo.
-
0:37
Irei em Advanced Help, Views, onde a documentação de Views está
-
0:40
no meu site e eu irei na seção Views API.
-
0:45
Há uma visão geral aqui, e se rolarmos para baixo
-
0:48
você verá que a primeira coisa mais importante que precisamos
-
0:52
é hook_views_api, o qual precisamos para declarar
-
0:56
a Views que, ei, estamos usando Views, qual versão estamos usando?
-
1:02
Agora, também precisamos ver que há documentação para esse hook
-
1:06
em api.drupal.org então podemos ir em frente
-
1:09
e escrever isso na minha pesquisa aqui
-
1:12
e você verá que é o primeiro aqui, então vou prosseguir e selecionar isso.
-
1:16
Após carregar, você verá que temos a documentação vinda
-
1:20
direto do código ao invés de uma página de livro,
-
1:25
e um exemplo de código, que é bastante útil
-
1:28
quando estiver trabalhar com hooks porque podemos usar, copiar/colar.
-
1:31
Okay, irei voltar para a minha documentação no site aqui,
-
1:34
e iremos dar uma olhada nesse hook.
-
1:37
Você verá que temos três elementos, e o primeiro e único que é
-
1:41
obrigatório é a versão da API.
-
1:43
Precisamos dizer a Views qual versão da API estamos usando
-
1:47
para que possamos ter certeza que estamos usando as funções certas
-
1:49
e que as coisas estarão lá quando esperamos que estejam.
-
1:53
No Drupal 6 tínhamos a versão 2 e 3.
-
1:55
No Drupal 7 temos a versão 3.
-
1:57
Essa é a versão que vamos usar nesse vídeo,
-
2:00
e Views pode ser bastante inteligente sobre isso, e se há novas versões
-
2:04
mas suas coisas ainda funcionam, ótimo, mas definitivamente temos que declarar
-
2:08
qual é a versão da API.
-
2:10
A próxima coisa na lista aqui é o path, e precisamos disso apenas se
-
2:14
iremos incluir arquivos adicionais para nossas views
-
2:19
que não estão diretamente na pasta raíz do módulo.
-
2:22
Se iremos criar uma estrutura hierárquica nas nossas pastas
-
2:25
precisamos definir isso aqui, e depois temos o path do template
-
2:29
o qual iremos ver quando chegarmos em theming.
-
2:31
Não iremos tratar disso agora.
-
2:34
Irei no meu sites/all/modules.
-
2:38
Aí é onde todos meus módulos estão.
-
2:40
Eu criei uma pasta custom, e tenho o módulo drupalizeme que eu já criei,
-
2:44
que consiste de um arquivo info, o arquivo info padrão,
-
2:48
mas com uma dependência de Views porque estamos usando a Views API, certo?
-
2:53
E tenho um arquivo module vazio,
-
2:55
então esse é o esqueleto para criação de um módulo,
-
2:59
e aqui é onde iremos colocar código Views
-
3:02
que iremos escrever nesse vídeo e no próximo.
-
3:05
Irei começar fazendo um rápido DocBlock aqui para meu hook.
-
3:11
Essa é a convenção de código padrão para escrever um DocBlock para um hook,
-
3:16
e agora irei em frente e vou colocar meu hook.
-
3:18
Agora, eu posso escrever isso. É relativamente simples.
-
3:21
Mas eu realmente gosto de trabalhar com a documentação da API com hooks
-
3:25
porque sempre há exemplos de código que posso copiar/colar
-
3:28
e depois editar do jeito que preciso,
-
3:31
então irei voltar aqui e colar isso e colocar aqui,
-
3:35
e agora eu preciso consertar minha indentação rapidamente aqui.
-
3:39
Okay, então a primeira coisa que fazemos
-
3:44
quando estamos usando um hook é mudar a palavra hook para o nome do nosso módulo,
-
3:47
então isso é drupalizeme_views_api() agora.
-
3:51
Temos isso nomeado apropriadamente, e agora vamos revisar
-
3:54
os itens que temos aqui.
-
3:56
Como eu disse, a versão da API que estamos declarando é 3,
-
4:00
então isso é relativamente direto
-
4:04
e fácil de fazer aqui.
-
4:06
O path, conforme eu disse, irei colocar -
-
4:10
os arquivos adicionais que irei criar para isso,
-
4:14
irei coloca-los numa estrutura de pastas hierárquica
-
4:17
apenas para organizar as coisas do jeito que eu quero.
-
4:19
Você não precisa fazer isso se você não quiser.
-
4:21
Você pode colocar tudo diretamente na sua pasta do módulo,
-
4:24
mas irei mudar isso.
-
4:26
Eu quero fazer um drupal_get_path para meu módulo -
-
4:28
o módulo example não existe - então o path vê o meu módulo,
-
4:33
e então irei criar pastas include e views
-
4:36
uma vez que chegarmos nesse passo de criar minhas views padrão.
-
4:40
E o template path, não iremos tratar disso agora.
-
4:42
Novamente, isso se trata mais de theming se o meu trabalho em Views
-
4:45
está fornecendo um template para tema, e eu não irei fazer isso agora,
-
4:50
então podemos deixar isso de lado por enquanto.
-
4:52
A última coisa que precisamos fazer para assegurar que todas essas coisas
-
4:55
estão funcionando, tudo que eu fiz foi declarar uma versão de API,
-
4:58
e eu disse que tenho um path para outros arquivos que você talvez esteja interessado,
-
5:02
mas a última coisa que precisamos fazer é na verdade habilitar o módulo.
-
5:05
Isso não irá fazer nenhum bem para mim se não habilitarmos em algum momento,
-
5:08
então irei em frente e irei voltar ao website,
-
5:11
e iremos aqui em Modules
-
5:15
e depois rolar para baixo, e devemos ver
-
5:21
o meu módulo Drupalize.Me,
-
5:24
então iremos em frente e habilitaremos isso.
-
5:26
E você verá que uma vez que o módulo é habilitado, é habilitado.
-
5:29
Nada aconteceu porque tudo que fizemos foi declarar nossa versão da API.
-
5:33
Não fizemos nada ainda, então isso é o que iremos fazer
-
5:38
em seguida, vamos fazer algo com a Views API.
-
0:00
[Views'e eklentinizi tanıtmak] [Drupal 7 Views için kod yazımı]
-
0:02
[Bölüm 4] [Addison Berry]
-
0:04
Bu videoda Views API ile çalışmaya başlayacağız.
-
0:08
Views API ile bir şekilde çalışacaksanız. İlk olarak yapmanız
-
0:11
gereken Views'e bundan bahsetmektir.
-
0:15
Çok basit bir eklenti oluşturarak başlayacağız.
-
0:18
ve ilk olarak da hook_views_api tanımlamasıyla başlıyoruz.
-
0:24
Viewse hangi API'yı kullandığımızı söylemek için.
-
0:28
Herhangibir kod yazmaya başlamadan önce yapmamız gereken,
-
0:31
bizim için yazılmış bir döküman olup olmadığına bakmalıyız.
-
0:34
Bu şekilde ne yapacağımıza daha rahat karar veririz.
-
0:37
Gelişmiş yardım kısmına gidiyorum. Sitemde Views yardımının olduğu kısma ,
-
0:40
buradan da Views API kısmına gidiyorum.
-
0:45
Genel bir bilgilendirme var burada. Aşağı gidiyorum.
-
0:48
Gördüğünüz gibi başlarken kullanmamız gereken ilk ve en önemli hook ,
-
0:52
hook_views_api. Bu hook ile
-
0:56
Views API'sini kullanacağımızı ve hangi versiyonunu kullanacağımızı Views'e söylemiş oluyoruz.
-
1:02
Şimdi ayrıca bu hook için api.drupal.org'da döküman da var.
-
1:06
Ve ben de gidip
-
1:09
buraya yazıyorum.
-
1:12
Bakın ilk olan, bunu seçiyorum.
-
1:16
Yükleniyor, gördüğünüz gibi dökümanlar direk kod olarak
-
1:20
geliyor, bir book sayfası olarak değil.
-
1:25
Ve buradaki kod örneği, hook lar ile çalışırken çok
-
1:28
kullanışlı çünkü buradan kopyala/yapıştır yapabiliriz.
-
1:31
Evet, Web sitemin döküman kısmına dönüyorum tekrar.
-
1:34
ve bu hook'a bakıyoruz.
-
1:37
Bakın burada 3 üç element var. İlk ve zorunlu olan
-
1:41
buradaki API versiyonu.
-
1:43
Views'e hangi API versiyonunu söylememiz gerekiyor.
-
1:47
doğru fonksiyonları yazabilmemiz için.
-
1:49
herşeyin beklediğimiz gibi çalıştığından emin olmak için.
-
1:53
Drupal 6 için 2 ve 3 versiyonları var.
-
1:55
Drupal 7 için versiyon 3 kullanıyoruz.
-
1:57
Bu videoda da versiyon 3 kullanacağız.
-
2:00
Views bu konuda yeterinde zeki, yeni versiyon olsa da kodlarınız çalışıyor.
-
2:04
Harika. Ama Hangi API versiyonunu kullanacağımızı
-
2:08
belirtmek zorundayız.
-
2:10
Bu listedeki diğer kısım yol. Eğer Dosyalarımızı farklı klasörlere koyacaksak
-
2:14
bu kısma ihtiyacımız var.
-
2:19
Eklentinin kendi klasörü için gerek yok.
-
2:22
eğer dosyalarımız için hiyerarşik bir düzen yapacaksak,
-
2:25
Burada belirtiyoruz. Burada templateyolu var.
-
2:29
Buna ise tema kısmında bakacağız.
-
2:31
Şimdi bununla ilgilenmiyoruz.
-
2:34
Sites, all modules klasörüne gidiyorum.
-
2:38
Tüm eklentilerim burada.
-
2:40
custom diye bir klasör ekledim. burada da drupalize.me adında klasörüm var.
-
2:44
Burada standart bir .info dosyam var.
-
2:48
Ama Views eklentisi gerekli çünkü Views API kullanacağız dimi ?
-
2:53
Ve boş olan module dosyam var.
-
2:55
Bunlar eklenti oluşturmanın en temel adımları.
-
2:59
Views kodlarımızı bulunduracağımız yer de burası.
-
3:02
Bu ve diğer videoda yapacağımız gibi.
-
3:05
Burada hook için hızlıca bir açıklama bloğu ekliyorum.
-
3:11
Bir hook ekliyorsak standart olarak bu açıklamayı ekleriz mutlaka.
-
3:16
Ve şimdi de hook'umu ekliyorum.
-
3:18
Şimdi yazabilirim gayet basit.
-
3:21
Ama Views hooklarıyla çalışmak gayet eğlenceli
-
3:25
çünkü her zaman kopyala yapıştır yapabiliyorsunuz.
-
3:28
Sonrada gerekli kısımları düzenliyorsunuz.
-
3:31
Simdi de gelip bunu buraya yapıştırıyorum.
-
3:35
İndent yapısını düzelteyim hemen.
-
3:39
Tamam, şimdi ilk yapmamız gereken şey.
-
3:44
Hook'larla çalışırken hook ismini modül ismi ile değiştiririz.
-
3:47
yani drupalize.me_views_api olacak.
-
3:51
İsmimizi verdik şimdi elementlere bakalım
-
3:54
buradakilere bakalım.
-
3:56
Dediğim gibi, API versiyonumuz 3 olacak.
-
4:00
Anlaşıldığı üzere.
-
4:04
Yapması kolay gayet.
-
4:06
Yol kısmına da dediğim gibi
-
4:10
ek klasörlerim olacak. Bunun için oluşturuyorum onları.
-
4:14
Hiyerarşik bir yapıda dosyalarım olacak.
-
4:17
İstediğim şekilde organize etmem için.
-
4:19
İstemiyorsanız yapmanıza gerek yok.
-
4:21
İstediğiniz herşeyi direk eklenti dosyanızın içine ekleyebilirsiniz.
-
4:24
Ama ben değiştiriyorum bunu.
-
4:26
Bunu drupal_get_path ile kendi eklentime yönlendiriyorum.
-
4:28
example eklentisi yok şu an. Şimdi yol benim klasörüme gidecek.
-
4:33
Sonrada includes ve views klasörleri ekliyorum.
-
4:36
Template yolu ve default views ekleyeceğimiz zaman
-
4:40
bu kısımla ilgileneceğiz. Şimdi bakmıyoruz.
-
4:42
Yine, eğer Viewsimiz bir template sağlayacaksa bu kısım
-
4:45
ile ilgileneceğiz. Şu an ben yapmayacağım bunları.
-
4:50
Şimdilik boş bırakabiliriz.
-
4:52
Her şeyin doğru çalıştığından emin olmak için yapmamız gereken
-
4:55
son şey, tüm yaptığımız API versiyonu belirtmek,
-
4:58
ve diğer dosyalarım için yolu belirtmek oldu.
-
5:02
Ama son yapmam gereken gidip eklentiyi aktif etmek tabi.
-
5:05
Eklentiyi açmazsak bize bir faydası olmaz bunun.
-
5:08
Gidip web siteme bakıyorum şimdi.
-
5:11
Eklentiler kısmına gidiyorum.
-
5:15
Aşağı geliyorum ve burada
-
5:21
drupalize.me eklentim olmalı.
-
5:24
Aktif ediyorum bunu.
-
5:26
Aktif ediyorum bu şekilde gördüğünüz gibi.
-
5:29
Herhangi bir şey yapmıyor şu anda çünkü sadece API versiyonu tanımladık.
-
5:33
Henüz aslında hiç bir şey yapmadık. Bundan sonraki yapacağımız ise
-
5:38
Views API ile birşeyler yapmak olacak.
Loading ...
-
Free % watched
-
Coming SoonFree