Skip to content

Commit

Permalink
Add URL field to Posts
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson committed Oct 27, 2020
1 parent c857799 commit 67f68da
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 11 deletions.
5 changes: 4 additions & 1 deletion .stylelintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": "stylelint-config-standard",
"ignoreFiles": ["**/*.js"]
"ignoreFiles": ["**/*.js"],
"rules": {
"no-descending-specificity": null
}
}
6 changes: 6 additions & 0 deletions assets/css/app.less
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ article {
.meta {
color: #626463;
font-variant: small-caps;

a {
font-variant: none;
color: inherit;
}
}
}

Expand Down Expand Up @@ -145,6 +150,7 @@ form {
input[type="text"],
input[type="password"],
input[type="email"],
input[type="url"],
select {
width: 100%;
border: @border-secondary;
Expand Down
26 changes: 26 additions & 0 deletions migrations/Version20201027085110.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20201027085110 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add URL to posts.';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE post ADD url LONGTEXT DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE post DROP url');
}
}
1 change: 1 addition & 0 deletions public/build/app.3b7b7810.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion public/build/app.9f75ce3f.css

This file was deleted.

2 changes: 1 addition & 1 deletion public/build/entrypoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"css": [
"/build/0.10bbd395.css",
"/build/app.9f75ce3f.css"
"/build/app.3b7b7810.css"
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion public/build/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"build/0.10bbd395.css": "/build/0.10bbd395.css",
"build/0.823a39ce.js": "/build/0.823a39ce.js",
"build/app.css": "/build/app.9f75ce3f.css",
"build/app.css": "/build/app.3b7b7810.css",
"build/app.js": "/build/app.76c8e643.js",
"build/runtime.js": "/build/runtime.d94b3b43.js"
}
1 change: 1 addition & 0 deletions src/Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function savePost(
$post = $id ? $postRepository->find($id) : new Post();
$post->setTitle($request->get('title'));
$post->setBody($request->get('body'));
$post->setUrl($request->get('url'));
$date = new DateTime($request->get('date'), new DateTimeZone('Z'));
$post->setDate($date);

Expand Down
17 changes: 17 additions & 0 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class Post
*/
private $file;

/**
* @ORM\Column(type="text", nullable=true)
*/
private $url;

public function __construct()
{
$this->setDate(new DateTime('now', new DateTimeZone('Z')));
Expand Down Expand Up @@ -120,4 +125,16 @@ public function setFile(?File $file): self
}
return $this;
}

public function getUrl(): ?string
{
return $this->url;
}

public function setUrl(?string $url): self
{
$this->url = $url;

return $this;
}
}
13 changes: 8 additions & 5 deletions src/Rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ public function getItem(Post $post): DOMElement
$description = $this->dom->createElement('description', $post->getBody());
$item->appendChild($description);

$url = $this->urlGenerator->generate(
'post_view',
['id' => $post->getId()],
UrlGeneratorInterface::ABSOLUTE_URL
);
$url = $post->getUrl();
if (!$url) {
$url = $this->urlGenerator->generate(
'post_view',
['id' => $post->getId()],
UrlGeneratorInterface::ABSOLUTE_URL
);
}
$item->appendChild($this->dom->createElement('link', $url));

$date = $post->getDate()->format(DateTime::RSS);
Expand Down
7 changes: 6 additions & 1 deletion templates/contact/view.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
<h2>Posts</h2>
<ol>
{% for post in contact.posts %}
<li><a href="{{ path('post_view', {id:post.id}) }}">P{{ post.id}}:</a> {{ post.title }}</li>
<li>
<a href="{{ path('post_view', {id:post.id}) }}">P{{ post.id}}:</a> {{ post.title }}
{% if post.url %}
<a href="{{ post.url }}">{{ post.url }}</a>
{% endif %}
</li>
{% endfor %}
</ol>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion templates/home.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<li class="post">
<header>
<h2>
<a href="{{ path('post_view', {id: post.id}) }}">
<a href="{% if post.url %}{{ post.url }}{% else %}{{ path('post_view', {id: post.id}) }}{% endif %}">
{% if post.title %}{{ post.title }}{% else %}Post {{ post.id }}{% endif %}
</a>
</h2>
Expand Down
6 changes: 6 additions & 0 deletions templates/post/form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
<textarea name="body" id="body" rows="15" cols="120">{{ post.body }}</textarea>
</span>
</p>
<p class="fields">
<span class="field size-1">
<label for="url">Original URL:</label>
<input type="url" name="url" id="url" value="{{ post.url }}" />
</span>
</p>
<p class="fields">
{% if post.file %}
<span class="field size-1">
Expand Down
4 changes: 4 additions & 0 deletions templates/post/view.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
<p class="meta">
By {{ post.author.name }}.
<time datetime="{{ post.date.format('Y-m-d\\TH:i:s\\Z') }}">{{ post.date.format('Y F j (l), g:iA') }}</time>
{% if post.url %}
<span class="url">URL: <a href="{{ post.url }}">{{ post.url }}</a></span>
{% endif %}
</p>

<div>{{ post.body|markdownToHtml }}</div>

{% if post.file %}
Expand Down

0 comments on commit 67f68da

Please sign in to comment.