Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
project
pub_options
Commits
48fa7aaf
Commit
48fa7aaf
authored
Jul 08, 2019
by
Jorge Calderon
Browse files
Ran php codesniffer and updated warnings and errors.
parent
46412d09
Changes
17
Hide whitespace changes
Inline
Side-by-side
README.md
0 → 100644
View file @
48fa7aaf
# Publishing options module README.md
## CONTENTS OF THIS FILE
*
Introduction
*
Requirements
*
Installation
*
Configuration
*
Maintainers
## INTRODUCTION
The publishing options module allows you to create your own custom promotion options.
## REQUIREMENTS
This module requires no modules outside of Drupal core.
## INSTALLATION
-
Install the Publishing options module as you would normally install a contributed Drupal
module. Visit https://www.drupal.org/node/1897420 for further information.
## CONFIGURATION
To administer publishing options, go to
`/admin/config/content/publishing-options`
#### Content types
After adding the desired amount of options, you can then go to
`/admin/structure/types`
and choose publishing
options for the desired content types.
Once a content type has the desired publishing options, you can select the publishing options available to the content
type.
#### Views
You can add a field, filter and contextual filter to a view for publishing options.
----------------------------------------------------------------------
###### Author/Maintainers
-
Jorge Calderon - https://www.drupal.org/u/geocalleo
config/install/publishing_options.settings.yml
deleted
100644 → 0
View file @
46412d09
publishing_options.install
View file @
48fa7aaf
...
...
@@ -5,9 +5,6 @@
* Install, update and uninstall functions for the node module.
*/
use
Drupal\Core\Field\BaseFieldDefinition
;
use
Drupal\user\RoleInterface
;
/**
* Implements hook_schema().
*/
...
...
@@ -106,7 +103,7 @@ function publishing_options_install() {
$database
=
\
Drupal
::
database
();
// Add a default entry.
$fields
=
[
'title'
=>
'Promote to blog'
,
'title'
=>
'Promote
d
to blog'
,
'created'
=>
time
(),
'modified'
=>
time
(),
];
...
...
@@ -115,7 +112,7 @@ function publishing_options_install() {
->
execute
();
// Add another entry.
$fields
=
[
'title'
=>
'Promote to gallery'
,
'title'
=>
'Promote
d
to gallery'
,
'created'
=>
time
(),
'modified'
=>
time
(),
];
...
...
publishing_options.module
View file @
48fa7aaf
<?php
/**
* @file
*/
/**
* Implements hook_entity_type_alter().
*/
...
...
publishing_options.views.inc
View file @
48fa7aaf
<?php
/**
* @file
*/
/**
* Implements hook_views_data().
*/
function
publishing_options_views_data_alter
(
array
&
$data
)
{
function
publishing_options_views_data_alter
(
array
&
$data
)
{
$publishing_options
=
\
Drupal
::
service
(
'publishing_options.content'
);
$publishing_options
=
$publishing_options
->
getPublishingOptions
();
foreach
(
$publishing_options
as
$publishing_option
)
{
...
...
@@ -15,7 +18,7 @@ function publishing_options_views_data_alter(array &$data)
'filter'
=>
[
'title'
=>
t
(
$publishing_option
->
title
),
'field'
=>
'nid'
,
'id'
=>
'publishing_options_filter'
'id'
=>
'publishing_options_filter'
,
],
'argument'
=>
[
'id'
=>
'publishing_options_contextual_filter'
,
...
...
@@ -28,8 +31,8 @@ function publishing_options_views_data_alter(array &$data)
'field'
=>
[
'title'
=>
t
(
$publishing_option
->
title
),
'field'
=>
'nid'
,
'id'
=>
'publishing_options_field'
]
'id'
=>
'publishing_options_field'
,
]
,
];
}
...
...
@@ -43,6 +46,6 @@ function publishing_options_views_data_alter(array &$data)
'base'
=>
'publishing_options_option_node'
,
'base field'
=>
'nid'
,
'field'
=>
'nid'
,
]
]
,
];
}
src/Controller/AdminPagesController.php
View file @
48fa7aaf
...
...
@@ -6,6 +6,7 @@ use Drupal\Core\Controller\ControllerBase;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Drupal\Core\Database\Connection
;
use
Drupal\Core\Url
;
use
Drupal\Core\Datetime\DateFormatter
;
/**
* An example controller.
...
...
@@ -19,13 +20,21 @@ class AdminPagesController extends ControllerBase {
*/
protected
$database
;
/**
* The date time object.
*
* @var \Drupal\Core\Database\Connection
*/
protected
$datetime
;
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
)
{
$controller
=
new
static
(
$container
->
get
(
'database'
)
);
$container
->
get
(
'database'
),
$container
->
get
(
'date.formatter'
)
);
$controller
->
setStringTranslation
(
$container
->
get
(
'string_translation'
));
return
$controller
;
}
...
...
@@ -36,8 +45,9 @@ class AdminPagesController extends ControllerBase {
* @param \Drupal\Core\Database\Connection $database
* The database connection.
*/
public
function
__construct
(
Connection
$database
)
{
public
function
__construct
(
Connection
$database
,
DateFormatter
$datetime
)
{
$this
->
database
=
$database
;
$this
->
datetime
=
$datetime
;
}
/**
...
...
@@ -45,22 +55,26 @@ class AdminPagesController extends ControllerBase {
*/
public
function
index
()
{
$header
=
[
[
'data'
=>
[
'data'
=>
$this
->
t
(
'ID'
),
'field'
=>
'po.pubid'
,
'class'
=>
[
RESPONSIVE_PRIORITY_LOW
],
'sort'
=>
'asc'
'sort'
=>
'asc'
,
],
[
'data'
=>
[
'data'
=>
$this
->
t
(
'Title'
),
'field'
=>
'po.title'
'field'
=>
'po.title'
,
],
[
'data'
=>
[
'data'
=>
$this
->
t
(
'Updated'
),
'field'
=>
'po.modified'
,
'class'
=>
[
RESPONSIVE_PRIORITY_LOW
]
'class'
=>
[
RESPONSIVE_PRIORITY_LOW
]
,
],
[
'data'
=>
[
'data'
=>
$this
->
t
(
'Operations'
),
'field'
=>
'operation'
,
],
...
...
@@ -69,17 +83,17 @@ class AdminPagesController extends ControllerBase {
->
extend
(
'Drupal\Core\Database\Query\TableSortExtender'
)
->
extend
(
'Drupal\Core\Database\Query\PagerSelectExtender'
);
$query
->
fields
(
'po'
,
[
'pubid'
,
'title'
,
'modified'
]);
$query
->
fields
(
'po'
,
[
'pubid'
,
'title'
,
'modified'
]);
$query
=
$query
->
orderByHeader
(
$header
);
$query
=
$query
->
limit
(
10
);
$results
=
$query
->
execute
();
// Initialize an empty array
// Initialize an empty array
.
$rows
=
[];
// Next, loop through the $results array
// Next, loop through the $results array
.
foreach
(
$results
as
$id
=>
$row
)
{
$row
->
modified
=
\
Drupal
::
service
(
'date.formatter'
)
->
format
(
$row
->
modified
,
'custom'
,
'M d, Y'
,
null
,
null
);
$row
->
modified
=
$this
->
datetime
->
format
(
$row
->
modified
,
'custom'
,
'M d, Y'
,
NULL
,
NULL
);
$row
=
(
array
)
$row
;
$row
[
$id
]
=
[
...
...
@@ -90,14 +104,14 @@ class AdminPagesController extends ControllerBase {
],
'#links'
=>
[
'edit'
=>
[
'title'
=>
t
(
'Edit'
),
'title'
=>
$this
->
t
(
'Edit'
),
'url'
=>
Url
::
fromRoute
(
'publishing_options.edit'
,
[
'id'
=>
$row
[
'pubid'
]]),
],
'delete'
=>
[
'title'
=>
t
(
'Delete'
),
'title'
=>
$this
->
t
(
'Delete'
),
'url'
=>
Url
::
fromRoute
(
'publishing_options.delete'
,
[
'id'
=>
$row
[
'pubid'
]]),
],
]
]
,
],
];
$rows
[]
=
[
'data'
=>
$row
];
...
...
@@ -105,19 +119,21 @@ class AdminPagesController extends ControllerBase {
// Build the table.
$build
=
[
'#markup'
=>
'<p>'
.
t
(
'The layout here is themed as a table
that is sortable by clicking the header name.'
)
.
'</p>'
,
'#markup'
=>
'<p>'
.
$this
->
t
(
'The layout here is themed as a table
that is sortable by clicking the header name.'
)
.
'</p>'
,
];
$build
[
'tablesort_table'
]
=
[
'#type'
=>
'table'
,
'#header'
=>
$header
,
'#rows'
=>
$rows
,
'#empty'
=>
t
(
'No publishing options found.'
)
'#empty'
=>
$this
->
t
(
'No publishing options found.'
)
,
];
$build
[
'pager'
]
=
[
'#type'
=>
'pager'
'#type'
=>
'pager'
,
];
return
$build
;
...
...
src/Form/PublishingOptionsDeleteForm.php
View file @
48fa7aaf
...
...
@@ -16,12 +16,12 @@ use Drupal\Core\Url;
class
PublishingOptionsDeleteForm
extends
ConfirmFormBase
{
/**
* Publishing option id
* Publishing option id
.
*/
protected
$id
;
/**
* Publishing options service
* Publishing options service
.
*/
protected
$publishing_options
;
...
...
@@ -43,8 +43,8 @@ class PublishingOptionsDeleteForm extends ConfirmFormBase {
*/
public
static
function
create
(
ContainerInterface
$container
)
{
$form
=
new
static
(
$container
->
get
(
'publishing_options.content'
)
);
$container
->
get
(
'publishing_options.content'
)
);
return
$form
;
}
...
...
@@ -99,13 +99,17 @@ class PublishingOptionsDeleteForm extends ConfirmFormBase {
*/
public
function
submitForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
\
Drupal
::
messenger
()
->
addStatus
(
$this
->
t
(
'The publishing option %title has been deleted.'
,
[
'%title'
=>
$this
->
publishing_options
->
title
(
$this
->
id
)])
$this
->
t
(
'The publishing option %title has been deleted.'
,
[
'%title'
=>
$this
->
publishing_options
->
title
(
$this
->
id
)]
)
);
$this
->
logger
(
'publishing_option'
)
->
notice
(
'publishing_option: deleted %title and all its associated content relationships.'
,
[
'%title'
=>
$this
->
publishing_options
->
title
(
$this
->
id
)]);
->
notice
(
'publishing_option: deleted %title and all its associated content relationships.'
,
[
'%title'
=>
$this
->
publishing_options
->
title
(
$this
->
id
)]
);
$this
->
publishing_options
->
delete
([
'pubid'
=>
$this
->
id
]);
...
...
src/Form/PublishingOptionsForm.php
View file @
48fa7aaf
<?php
namespace
Drupal\publishing_options\Form
;
use
Drupal\Core\Form\ConfigFormBase
;
use
Drupal\Core\Form\FormStateInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Drupal\node\Entity\NodeType
;
use
Drupal\publishing_options
\
Services\PublishingOptionsContent
;
use
Drupal\Core\Entity\EntityTypeManager
;
/**
* Implements an example form.
...
...
@@ -13,34 +14,38 @@ use Drupal\publishing_options\Services\PublishingOptionsContent;
class
PublishingOptionsForm
extends
ConfigFormBase
{
/**
* Publishing option id
* Publishing option id
.
*/
protected
$id
;
/**
* Publishing options service
* Publishing options service
.
*/
protected
$publishing_options
;
/**
* Entity type manager service.
*/
protected
$entity_type_manager
;
/**
* Construct the new form object.
*
* @param \Drupal\publishing_options\Services\PublishingOptionsContent $publishing_options
* The publishing options service.
*/
public
function
__construct
(
PublishingOptionsContent
$publishing_options
)
{
public
function
__construct
(
PublishingOptionsContent
$publishing_options
,
EntityTypeManager
$entity_type_manager
)
{
$this
->
publishing_options
=
$publishing_options
;
$this
->
entity_type_manager
=
$entity_type_manager
;
}
/**
* {@inheritdoc}
*
* We'll use the ContainerInjectionInterface pattern here to inject the
* current user and also get the string_translation service.
*/
public
static
function
create
(
ContainerInterface
$container
)
{
$form
=
new
static
(
$container
->
get
(
'publishing_options.content'
)
$container
->
get
(
'publishing_options.content'
),
$container
->
get
(
'entity_type.manager'
)
);
return
$form
;
...
...
@@ -66,10 +71,9 @@ class PublishingOptionsForm extends ConfigFormBase {
public
function
buildForm
(
array
$form
,
FormStateInterface
$form_state
,
$id
=
NULL
)
{
$form_values
=
$form_state
->
getValues
();
$node_types
=
NodeType
::
loadMultiple
(
);
$node_types
=
$this
->
entity_type_manager
->
getStorage
(
'node_type'
);
$form_values
[
'bundles'
]
=
[];
if
(
!
is_null
(
$id
))
{
$this
->
id
=
$id
;
...
...
@@ -82,21 +86,22 @@ class PublishingOptionsForm extends ConfigFormBase {
}
$options
=
[];
foreach
(
$node_types
as
$node_type
)
{
foreach
(
$node_types
->
loadMultiple
()
as
$node_type
)
{
$options
[
$node_type
->
id
()]
=
$node_type
->
label
();
}
if
(
!
isset
(
$form_values
[
'title'
]))
{
if
(
isset
(
$publishing_option
->
title
))
{
$form_values
[
'title'
]
=
$publishing_option
->
title
;
}
else
{
}
else
{
$form_values
[
'title'
]
=
''
;
}
}
if
(
!
empty
(
$options
)
&&
!
empty
(
$publishing_option
))
{
foreach
(
$publishing_option
->
bundles
as
$id
=>
$option
)
{
$form_values
[
'bundles'
][
$id
]
=
$option
;
$form_values
[
'bundles'
][
$id
]
=
$option
;
}
}
...
...
@@ -122,16 +127,14 @@ class PublishingOptionsForm extends ConfigFormBase {
'#submit'
=>
[
'::cancel'
],
];
return
parent
::
buildForm
(
$form
,
$form_state
);
return
parent
::
buildForm
(
$form
,
$form_state
);
}
/**
*
*
{@inheritdoc)
*/
public
function
cancel
(
array
&
$form
,
FormStateInterface
$form_state
)
{
$form_state
->
setRedirect
(
'publishing_options.index'
);
// return new Url('publishing_options.index');
}
/**
...
...
@@ -141,7 +144,7 @@ class PublishingOptionsForm extends ConfigFormBase {
$title
=
$form_state
->
getValue
(
'title'
);
if
(
strlen
(
$title
)
<
3
)
{
// Set an error for the form element with a key of "title".
$form_state
->
setErrorByName
(
'title'
,
$this
->
t
(
'The title must be
at
greater than 15 characters long.'
));
$form_state
->
setErrorByName
(
'title'
,
$this
->
t
(
'The title must be greater than 15 characters long.'
));
}
}
...
...
@@ -150,7 +153,6 @@ class PublishingOptionsForm extends ConfigFormBase {
*/
public
function
submitForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
$form_values
=
$form_state
->
getValues
();
$this
->
publishing_options
->
insert
(
$form_values
);
$this
->
messenger
()
->
addMessage
(
$this
->
t
(
'Publishing option %title has been added.'
,
[
'%title'
=>
$form_values
[
'title'
]]));
...
...
src/Form/PublishingOptionsNodeDeleteForm.php
View file @
48fa7aaf
...
...
@@ -2,8 +2,6 @@
namespace
Drupal\node\Form
;
use
Drupal\node\Form\NodeDeleteForm
;
/**
* Provides a form for deleting a node.
*
...
...
@@ -15,31 +13,39 @@ class PublishingOptionsNodeDeleteForm extends NodeDeleteForm {
* {@inheritdoc}
*/
protected
function
getDeletionMessage
()
{
/** @var \Drupal\node\NodeInterface $entity */
/**
* @var \Drupal\node\NodeInterface $entity
*/
$entity
=
$this
->
getEntity
();
$node_type_storage
=
$this
->
entityManager
->
getStorage
(
'node_type'
);
$node_type
=
$node_type_storage
->
load
(
$entity
->
bundle
())
->
label
();
if
(
!
$entity
->
isDefaultTranslation
())
{
return
$this
->
t
(
'@language translation of the @type %label has been deleted.'
,
[
'@language'
=>
$entity
->
language
()
->
getName
(),
'@type'
=>
$node_type
,
'%label'
=>
$entity
->
label
(),
]);
return
$this
->
t
(
'@language translation of the @type %label has been deleted.'
,
[
'@language'
=>
$entity
->
language
()
->
getName
(),
'@type'
=>
$node_type
,
'%label'
=>
$entity
->
label
(),
]
);
}
return
$this
->
t
(
'The @type %title has been deleted.'
,
[
'@type'
=>
$node_type
,
'%title'
=>
$this
->
getEntity
()
->
label
(),
]);
return
$this
->
t
(
'The @type %title has been deleted.'
,
[
'@type'
=>
$node_type
,
'%title'
=>
$this
->
getEntity
()
->
label
(),
]
);
}
/**
* {@inheritdoc}
*/
protected
function
logDeletionMessage
()
{
/** @var \Drupal\node\NodeInterface $entity */
/**
* @var \Drupal\node\NodeInterface $entity
*/
$entity
=
$this
->
getEntity
();
$this
->
logger
(
'content'
)
->
notice
(
'@type: deleted %title.'
,
[
'@type'
=>
$entity
->
getType
(),
'%title'
=>
$entity
->
label
()]);
}
...
...
src/Form/PublishingOptionsNodeForm.php
View file @
48fa7aaf
...
...
@@ -12,7 +12,6 @@ use Drupal\Core\Session\AccountInterface;
use
Drupal\Core\TempStore\PrivateTempStoreFactory
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Drupal\publishing_options
\
Services\PublishingOptionsContent
;
use
Drupal\node\NodeInterface
;
/**
* Form handler for the node edit forms.
...
...
@@ -36,7 +35,7 @@ class PublishingOptionsNodeForm extends NodeForm {
protected
$currentUser
;
/**
* Publishing options service
* Publishing options service
.
*/
protected
$publishing_options
;
...
...
@@ -67,14 +66,14 @@ class PublishingOptionsNodeForm extends NodeForm {
*/
public
static
function
create
(
ContainerInterface
$container
)
{
return
new
static
(
$container
->
get
(
'entity.repository'
),
$container
->
get
(
'tempstore.private'
),
$container
->
get
(
'entity_type.bundle.info'
),
$container
->
get
(
'datetime.time'
),
$container
->
get
(
'current_user'
),
$container
->
get
(
'date.formatter'
),
$container
->
get
(
'publishing_options.content'
)
);
$container
->
get
(
'entity.repository'
),
$container
->
get
(
'tempstore.private'
),
$container
->
get
(
'entity_type.bundle.info'
),
$container
->
get
(
'datetime.time'
),
$container
->
get
(
'current_user'
),
$container
->
get
(
'date.formatter'
),
$container
->
get
(
'publishing_options.content'
)
);
}
/**
...
...
@@ -85,9 +84,8 @@ class PublishingOptionsNodeForm extends NodeForm {
$type
=
$node
->
getType
();
// Get all publishing types available by this entity type
// Get all publishing types available by this entity type
.
$publishing_options
=
$this
->
publishing_options
->
getPublishingOptions
();
$previous_machine_name
=
null
;
// Check to see that publishing types are allowed for this entity.
foreach
(
$publishing_options
as
$publishing_option
)
{
...
...
@@ -95,26 +93,24 @@ class PublishingOptionsNodeForm extends NodeForm {
// Get publishing option associated with bundle.
$allowed_publishing_option
=
$this
->
publishing_options
->
getPublishingOptionBundles
(
$type
,
$publishing_option
->
pubid
);
// Create drupal machine name for publishing option
// Create drupal machine name for publishing option
.
$machine_name
=
strtolower
(
str_replace
(
' '
,
'_'
,
$publishing_option
->
title
));
}
else
{
$allowed_publishing_option
=
false
;
}
}
else
{
$allowed_publishing_option
=
FALSE
;
}
// If allowed publishing option returns a value.
if
((
bool
)
$allowed_publishing_option
)
{
// Set the publishing option checkbox.
$options
[
$publishing_option
->
pubid
]
=
$publishing_option
->
title
;
// Get the node if it has bundle association.
$node_association_exists
=
$this
->
publishing_options
->
getNode
(
$node
->
id
(),
$publishing_option
->
pubid
);
// Check if this node is already related to publishing option.
// If not, than
// If not, than
.
if
(
isset
(
$node_association_exists
->
selected
))
{
// If node is associated to publishing option, get selected state.
$value
=
(
bool
)
$node_association_exists
->
selected
;
}
else
{
}
else
{
$value
=
(
bool
)
$allowed_publishing_option
;
}
...
...
@@ -140,11 +136,11 @@ class PublishingOptionsNodeForm extends NodeForm {
// Get node entry.
$node
=
$this
->
entity
;
// Get all publishing types available by this entity type
// Get all publishing types available by this entity type
.
$publishing_options
=
$this
->
publishing_options
->
getPublishingOptions
();
// Iterate through all publishing options
foreach
(
$publishing_options
as
$id
=>
$publishing_option
)
{
// Iterate through all publishing options
.
foreach
(
$publishing_options
as
$publishing_option
)
{
// Get machine name of publsihing option.
$machine_name
=
strtolower
(
str_replace
(
' '
,
'_'
,
$publishing_option
->
title
));
...
...
@@ -158,4 +154,5 @@ class PublishingOptionsNodeForm extends NodeForm {
}
}
}
}
src/Form/PublishingOptionsNodeTypeDeleteConfirm.php
View file @
48fa7aaf
...
...
@@ -15,7 +15,7 @@ use Drupal\publishing_options\Services\PublishingOptionsContent;
class
PublishingOptionsNodeTypeDeleteConfirm
extends
NodeTypeDeleteConfirm
{
/**
* Publishing options service
* Publishing options service
.
*/
protected
$publishing_options
;
...
...
@@ -37,8 +37,8 @@ class PublishingOptionsNodeTypeDeleteConfirm extends NodeTypeDeleteConfirm {
*/
public
static
function
create
(
ContainerInterface
$container
)
{
$form
=
new
static
(
$container
->
get
(
'publishing_options.content'
)
);
$container
->
get
(
'publishing_options.content'
)
);
return
$form
;
}
...
...
@@ -51,11 +51,11 @@ class PublishingOptionsNodeTypeDeleteConfirm extends NodeTypeDeleteConfirm {
$type
=
$this
->
entity
;